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

Ads by Google

77 comments on “Java Static Import

  1. 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()

  2. 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.

  3. Hi,

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

  4. Hi,

    This information on java static import is very good..

  5. Thanx for explaining in such a simple manner.

    —– krunal shah

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

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

  8. Ads by Google
  9. 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 …

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

  11. 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..

  12. 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?

  13. Hi,

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

  14. 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.

  15. Very Good Information..Thanks for sharing

  16. 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!

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

    Thank you..

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

  19. Good Explanation…

    Thanks for it

  20. 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 :-(

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

  22. Excellent write up.. A bunch of thanks….

  23. 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

  24. I didn’t know this. it can be so useful if used sensibly. Nice article.

  25. Thanks for the notes. You have explained the concept in a good way.

  26. Thank you for sharing.Clean explanation.

  27. I have read many java books/articles/material but java static import is something I learned first time, as they mentioned the importance , scenarios and in very simple language.

  28. I have read many java books/articles/material but java static import is something I learned first time, as they mentioned the importance , scenarios and in very simple language.

  29. I have read many java books/articles/material but java static import is something I learned first time, as they mentioned the importance , scenarios and in very simple language.

  30. Hi Joe;
    While commenting here it dnt shows the successful insertion of comment and thus I tried few times and when reload the page it shows four times comments insertion.
    Please look after this.
    :Abhiroop

  31. Hi Joe

    As usual, nice and simple explanation.

    Great Job.

    -Prasad

  32. good explanation.
    Easy word and easy to learn.
    thanks….

  33. Nice explanation….. thanks a lot… really…..

    I know Core Java So good. but i want to learn a lot about it..
    So can anyone suggest me a very very difficult book over core java…..?

  34. Thanks for explaining in very simple way :)

  35. Sir,
    I’ve one dought, why we need not create object for static method ?

  36. Sir,
    I’ve one dought, why we need not create object for static method ?

  37. Keep to good work going Joe! I find your blog informative and like the way you write.

    Cheers,
    Bharat Sharma

  38. i have bumped into a treasure of a JAVA BLOG!!
    Thanks Joe :)

  39. The way you have explained makes one never forget the topic. Thanks Joe.

  40. nice presentation till now i didn’t heard about this static import yes i agree with you about ofter period of time we cant trace the application if using many static import in a single class
    thanks

  41. Is static import supports only java.lang.Math package

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

ABOUT
I am Joe, author of this blog. I run javapapers with loads of passion. If you are into java, you may find lot of interesting things around.
Ads by Google
STAY in TOUCH:

Email:

Core Java | Servlet | JSP | Design Patterns | Android | Spring | Web Service | © 2008-2012 javapapers.