«Back to Home

Core Java

Topics

Static Import In Java

Static Import
 
In Java, the static import feature of Java 5 makes easy for the programmer to access any static member of a class directly. There is no need to qualify it by the class name.
 
In other words, the normal import statement imports classes from the packages. Thus, they can be used without package reference. Similarly, the static import statement imports the static members from the classes and allow them to be used without the class reference. Less coding is required. It makes the program unreadable and unmaintainable.
 
Let’s see an example, given below.
 
Code
  1. import static java.lang.System.*;  
  2. public class StaticImport {  
  3.     public static void main(String args[]) {  
  4.         out.println("Harry");  
  5.         out.println("Bob");  
  6.     }  
  7. }  
54
 
Output

55

Difference between import and static import

Import

The import allows the programmer to access the classes of a package without the package qualification and it provides an accessibility to the classes and the interface.
 
Static Import

The static import allows the programmer to access the static members of a class without the class qualification and it provides an accessibility to the static members of the class.
 
Summary

Thus, we learnt that the static import feature of Java 5 makes easy for the programmer to access any static member of a class directly. There is no need to qualify it by the class name and also learnt the difference between import and static import in Java.