static import

Posted: July 20, 2012 in Java

Static Import is a new feature added in Java 5 specification. Java 5 has been around the corner for some time now, still lot of people who are new in Java world doesn’t know about this feature.

Although I have not used this feature in my work, still it is interesting to know about.

What is Static Import?

In order to access static members, it is necessary to qualify references with the class they came from. For example, one must say:

double r = Math.cos(Math.PI * theta);

or

System.out.println(“Blah blah blah”);

 

You may want to avoid unnecessary use of static class members like Math. and System. For this use static import. For example above code when changed using static import is changed to:

import static java.lang.System.out;

import static java.lang.Math.PI;

import static java.lang.Math.cos;

double r = cos(PI * theta);

out.println(“Blah blah blah”);

So whats the advantage of using above technique? Only advantage that I see is readability of the code. Instead of writing name of static class, one can directly write the method or member variable name.

Also keep one thing in mind here. Ambiguous static import is not allowed. i.e. if you have imported java.lang.Math.PI and you want to import mypackage.Someclass.PI, the compiler will throw an error. Thus you can import only one member PI.

A static import declaration has two forms—one that imports a particular static member (which is known as singlestatic import) and one that imports all static members of a class (which is known as static import on demand). The following syntax imports a particular static member:

import static packageName.ClassName.staticMemberName;

where packageName is the package of the class (e.g., java.lang), ClassName is the name of the class (e.g., Math) andstaticMemberName is the name of the static field or method (e.g., PI or abs). The following syntax imports all static members of a class:

import static packageName.ClassName.*;

where packageName is the package of the class (e.g., java.lang) and ClassName is the name of the class (e.g., Math). The asterisk (*) indicates that all static members of the specified class should be available for use in the class(es) declared in the file. Note that static import declarations import only static class members. Regular import statements should be used to specify the classes used in a program.

 

In order to use the static import flavor of java. We have to declare all the variables and methods as static and public for availability and accessing methods and variables using Class name. its better to call it import static to avoid syntactical errors. See the examples below.

package aaa.bbb.ccc;

public class sss {

public static final String xxx=”hello”;

public static int mone(){

return 10;

}

}

 

package ccc.bbb.aaa;

import static aaa.bbb.ccc.sss.*;

import static java.lang.System.out;

public class Example {

public static void main(String[] args) {

out.println(xxx);

out.println(mone());

}

}

Output:

hello

10

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s