Tuesday, 31 March 2015

Static Import in Java

Static Import:

The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.

Advantage of static import:

  • Less coding is required if you have access any static member of a class oftenly.

Disadvantage of static import:

  • If you overuse the static import feature, it makes the program unreadable and unmaintainable.

Simple Example of static import

1.    import static java.lang.System.*;    
2.    class StaticImportExample{  
3.      public static void main(String args[]){  
4.         
5.       out.println("Hello");//Now no need of System.out  
6.       out.println("Java");  
7.      
8.     }   
9.    }  


Output:Hello
       Java
  

What is the difference between import and static import?


The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification. The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...