Java Static Import

14/06/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!

Java Static Import

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

Gr8 work………Thanks a lot

GJ on August 10th, 2010 6:47 am

very nice

f on August 13th, 2010 1:57 pm

Thanx a lot. Very informative keep on

Surendra on September 15th, 2010 9:34 am

Hi this is best answer we get on java related.
So keep trying to work good,better and best.

Devendra on October 21st, 2010 12:55 pm

there is an interesting thing, that you have not mentioned,

what is the difference between:

1.
————————
import java.io.*;

class main
{
public static void main(String[] args)
{
FileWriter fw = null;
}
}
————————

2.
————————
import java.io.FileWriter;

class main
{
public static void main(String[] args)
{
FileWriter fw = null;
}
}
————————

most programmers don’t like to use the second version, which doesn’t waste resources, and cut wasted times, also .. the second option might be the only option, when you have either to use a private static member, or when the class is private !

thank you …

khaled khunaifer on November 22nd, 2010 5:34 pm

very good explanation about static import.I came across this site while searching for this topic in google.It is very interesting

Anonymous on December 9th, 2010 11:30 am

good explanation

harish on December 14th, 2010 7:17 am

very good explanation

mahidhar kundella on December 23rd, 2010 2:58 pm

Detailed explanation. Thanks.

Kumar on January 3rd, 2011 6:53 am

nice answer….
thnx..

chayan on January 10th, 2011 5:43 pm

i dont understand

why do we want to declare a final static variable in an interface and then static import t into another class

cant we directly declare a static final variable in the class itself and use it in the class…

less import hassles..

Mayank on March 16th, 2011 2:37 pm

Vary good explanation. Refer the below link for another example.

http://www.javabeat.net/qna/29-what-is-static-import-in-java-50/

Santhosh.J on April 15th, 2011 8:57 am

Quite informative

Aarya on May 6th, 2011 11:46 am

I do not fully understand: when you import static you import the static fields and methods from a package AND the classes in that package, or you import ONLY the static stuff?

Could someone answer to this, please?

Margu on May 17th, 2011 12:57 pm

Hi,

how static import is useful to achieve real time concepts means on real time objects…

ambrish on June 13th, 2011 6:00 pm

Very nice!!

Arivoli on August 4th, 2011 7:14 am

Previously I had a doubt about how does java inbuilt “System” class uses the Object Reference “out” of PrintStream class and “in” of InputStream class without using import statement. Now your post answers it. Thanks alot.

vidyasagar on August 29th, 2011 10:09 am

its all new features in java for me. keep up the good wor

Waqas on September 13th, 2011 11:55 am

Very Good Information..Thanks for sharing

Anonymous on September 14th, 2011 3:43 am

may I request you to add your profile link – ‘About me’ in menu list that gives quick access to read about your profile as you are helping lot so readers must be interested to look at your profile…
Thanks!

Anonymous on September 14th, 2011 3:51 am

excellent!!

Anu on September 30th, 2011 3:50 pm

I liked your way of writing..to the point!!

Thank you..

Anonymous on October 21st, 2011 2:09 pm

Can Static import will import static nested classes or nested enums or nested interfaces into our class????????

chander kumar on November 4th, 2011 7:28 am

Good Explanation…

Thanks for it

Nagabhooshanam on November 12th, 2011 3:28 pm

Hey dude,
The code you have given:
———————————-
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);
}
}

————–
This doesnt work:
It gives error as unable to find Symbol when I run MyMath :-(

Juben on November 22nd, 2011 9:46 am

in java which statement come first?import statement or static import statement.

nitinvavdiya on December 7th, 2011 6:18 am

Excellent write up.. A bunch of thanks….

Gopal on December 20th, 2011 10:20 am

Nice explanation…

senthilkumar on January 5th, 2012 11:53 am

Good explaination…

Ranjana on January 28th, 2012 1:21 pm

Hi,
I wanted to ask if using import copies and pastes the content of the entire class in my file, just like in c
Thanks

avi on February 1st, 2012 8:01 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home