Static Imports in Java 5 - Java
Home » Java, Tech Notes

Static Imports in Java 5

10 July 2009 No Comment

Static keyword is used to allow access to an attribute/ a method or a class without creating instance of it. (Some of you may argue that this is not object oriented way, but we leave that discussion aside for now.) We use static for those elements which do not change from instance to instance of a class. An example would be constant variables in a program. These values are stored using ‘public static final’ keywords. In enum article we have seen one way of managing logically linked constants. Static import tries to improve the way we store and use static elements in program.

Java says it has tried to address an anti-pattern – using interface for constants. Some of us may have used this type of design in programs, where constants are grouped in an interface and used by implementing that interface. This approach is wrong because it alters the purpose of having interfaces in a Java application. Interfaces are used to define highest level of abstract behavior and also to expose a contract to the world in terms of method signatures. Thus, we cannot use interfaces to store constants. An alternative is having them in respective functional classes or in a separate class which can be reused.

When we use this class, we import it and use it wherever the variable is required. Here is the example.

//Constants class

package constants;

public class MyConstants {

	public static final String CONSTANT1 = "constant 1";
	public static final String CONSTANT2 = "constant 2";
	public static final String CONSTANT3 = "constant 3";
}


//Class using constants without static import

import constants.MyConstants;

public class WithoutStaticImport {

	public static void main(String[] args) {
		System.out.println(MyConstants.CONSTANT1);
	}

}

If you have many such constants, then every time you need to use these constants with class name in front of it. With static import, you can get rid of the class name and use constants directly. Let us see how.

//Using Static Constant

import static constants.MyConstants.CONSTANT1;

public class WithStaticImport {

	public static void main(String[] args) {
		System.out.println(CONSTANT1);
	}

}

Similar to static import of a variable, you can import all constant attributes of the MyConstant class using following syntax.

//Importing all constant (static) attributes

import static constants.MyConstants.*;

public class WithStaticImport {

	public static void main(String[] args) {
		System.out.println(CONSTANT1);
	}

}

Important Considerations:

  • Java suggests – use static imports sparingly because these reduce readability and maintainability of a program considerably.
  • Use it when you feel you are going to wrongly use inheritance (i.e. interfaces).
  • When number of variables is less and you can maintain the readability by using meaningful names.

Previous Next

 

More Related Posts in Java, Tech Notes

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.