Saturday, May 11, 2013

Java static import

java import static

Static import in java is the feature which is available in Java 1.5.

This is the best feature if we want to use in some real time scenarios.

Before java 1.5 if we want to use the static members in another classes/packages

we are using the Classname.MemberName, For Example ClassA.Method();

But With this static import feature, we just define the classname/members starting with

import static pkg.class;
import static pkg.class.memberNames;

In the code we can directly use the static members, if we write static import.
But at the same time we cann't use more static members. in long run the new developer
may gets confused which member is for which class.

static imports in java Example:


 package javademos;  
 public class AppConstants {  
  public static int maxTimeLimit = 1 ;//in hours  
  public static int minTimeLimit = 1 ;//in hours  
 }  


 package javademos;  
 import static javademos.AppConstants.*;  
 // Importing all static members and using directly as shown below.
 public class StaticImport {  
      public static void main(String[] args) {  
           System.out.println("maxTimeLimit "+maxTimeLimit);  
           System.out.println("minTimeLimit "+minTimeLimit);  
      }  
 }  


0 comments:

Post a Comment

 
Disclaimer : If you find any mistakes / corrections / feedback Please let us know...This website author shall not accept liability or responsibility for any errors, mistakes or misstatements found in this website.