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
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
Important Considerations:
Here are some examples:
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); } }
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.
Here are some examples:
- instead of System.out.println you can use out.println:
Import the field:
import static java.lang.System.out;
So now in code you can write:out.println("Hello, World!");
- PI instead of Math.PI
look so much nicer thanarea = PI * R * R
area = Math.PI * R * R
No comments:
Post a Comment