Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Static Import

Wed Feb 04, 2009 12:48 am

Static Import usage in java :
Suppose you have a static member of a class included in an other package, instead of import the class you just can import the static member (Feature introduced in J2SE5) .
Hints about using static import:
  1. Use static keyword in after the import keyword .
  2. You can import static variables ,object references and methods .
  3. You can use the (.* ) to import all the static members .
  4. Members imported using static could be used without references or class names.
  5. Static import usually used to load constants to your class.
  6. Too many static imports in your class is harmful for code readability and maintenance in the future.

Without using static import


Using of variable limt which is a static member of HasStaticMember class.
java code
package CurveOr;
import CurveIT.HasStaticMember;

public class UsingStaticImport {
public static void main(String[] args) {
System.out.println(HasStaticMember.limt);
}
}

The class HasStaticMember is on another package "CurveIT":
java code
package CurveIT;
public class HasStaticMember {
public static int limt=100;
}


Keep in mind that the HasStaticMember class is not at the same package of the importing class.


Using static import with static variables


java code
package CurveOr;
import static CurveIT.HasStaticMember.limt;

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


As you can notice ,
java code
import static CurveIT.HasStaticMember.limt;

The upper statement is used to import static members .

Using static import with static functions


The class HasStaticFunction has a static function println() which we are going to apply a static import on it at UsingStaticImport class:
java code
package CurveIT; 
public class HasStaticFunction {
public static void println()
{
System.out.println("I am HasStaticFunction");
}

}

The usage :
java code
package CurveOr;
import static CurveIT.HasStaticFunction.println;

public class UsingStaticImport {
public static void main(String[] args) {
println();
}
}

As you see the function println it statically imported :
java code
import static CurveIT.HasStaticFunction.println;

and used directly using its name without any references or class name.

Importing static variables and functions at the same time :


You can all the static members in a class using only one static import statement as follows :
java code
package CurveIT;


public class HasStaticMember {
public static int limit=100;
public static void setLimit(int newlimt)
{

limit=newlimt;
}
}

Imported at:
java code
package CurveOr;
import static CurveIT.HasStaticMember.*;

public class UsingStaticImport {
public static void main(String[] args) {
setLimit(43);
System.out.println("Limit is"+limit);
}
}


The output of this code is :
Code:
Limit is 43




Re: Static Import

Wed Jun 12, 2013 5:14 pm

updated.

Post a reply
  Related Posts  to : Static Import
 difference between a static and a non-static inner class     -  
 import XSD file in another     -  
 Import text file with ASP.NET     -  
 import Mysql database     -  
 How to import your website dump database script     -  
 Import a PowerPoint presentation into Windows Movie Maker     -  
 What is a static method     -  
 static vs fixed div position     -  
 Static Methods and Variables     -  
 Class static properties     -  

Topic Tags

Java Basics