Wish to have some nice time? Check out what I'm reading online!

What is a static import in java?

June 14th, 2008

First lets understand what does “java import” does to your java program!
Consider the java import statements:
1) import package.ClassA;
2) import package.*;

Java statement (1) gives you a license to use ClassA inside the whole program without the package reference. That is you can use like ClassA obj = new ClassA(); or ClassA.getStatic(); Java statement (2) allows you to use all the java classes that belong the imported package in the above manner. That is without the package reference.

If you don’t use import statement, you can still use the classes of that package. But you should invoke it with package reference whereever you use.

That is like, package.ClassA obj = new package.ClassA(); – looks very ugly isn’t it?

Now coming to the static import part. Like the above ugly line we have been unknowingly using (abusing) the java static feature.

Consider the java example: double r = Math.cos(Math.PI * theta);

How about writing the same java code like: double r = cos(PI * theta); – looks more readable right?

This is where static import in java comes to help you.

import static java.lang.Math.PI;
import static java.lang.Math.cos;

Do the above static imports and then you can write it in the more readable  way!

Definition: The normal import declaration imports classes from packages, so that they can be used without package reference. Similarly the static import declaration imports static members from classes and allowing them to be used without class reference.

Now, we have got an excellent java feature from java 1.5. Ok now we shall see how we can abuse this!

Can i static import everything?
like, import static java.lang.Math.*; – yes it is allowed! Similarly you do for class import.

Please don’t use this feature, because over a period you may not understand which static method or static attribute belongs to which class inside the java program. The program may become unreadable.

General guidelines to use static java import:

1) Use it to declare local copies of java constants
2) When you require frequent access to static members from one or two java classes

Interesting , I never knew that you can import a static method in java. Looks cool. But to me , it would be easy to read / understand if we associate the class name. i.e Math.cos rather calling cos()

Divakar on November 17th, 2009 9:53 pm

Yes that is right. Avoid using java static import in general straight forward cases.

But consider using the java static import in following two scenarios:

1) When you have to only one or max two static imports in a class many number(say atleast 20) of times.
2) Constant Interface Antipattern

Example for above case 2):
When you create a interface with constants and extend it to use (note below ‘PI’ referring the constant). In this scenario, better use the bottom code.

public interface MyConstant {
public static final double PI = 3.14159;
}

public class MyMath implements MyConstant {
public double myComplexMathMethod() {
return (PI * 100);
}
}

————–

public final class MyConstant {
private MyConstant() {}
public static final double PI = 3.14159;
}

import static MyConstant.PI;
public class MyMath {
public double myComplexMathMethod() {
return (PI * 100);
}
}

http://en.wikipedia.org/wiki/Constant_interface – has good information related to the topic.

Joe on November 17th, 2009 11:23 pm

Hi,

I happened to see your post on “Static import in Java” and find it quite informative.
Thanks for sharing valuable knowmledge.
Bizz..

Bizz on January 22nd, 2010 10:14 am

Hi,

This information on java static import is very good..

Prashant Reddy on April 12th, 2010 5:00 am

Thanx for explaining in such a simple manner.

—– krunal shah

krunal shah on June 10th, 2010 5:32 am

Static import in java is new for me. I have never heard about it. Thanks man.

Priya on July 16th, 2010 6:55 pm





hidden and gravatar enabled.