Java Static Import

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

This Core Java tutorial was added on 14/06/2008.

Comments on "Java Static Import" Tutorial:

  1. Divakar says:

    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. Joe says:

    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. Bizz says:

    Hi,

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

  4. Prashant Reddy says:

    Hi,

    This information on java static import is very good..

  5. krunal shah says:

    Thanx for explaining in such a simple manner.

    —– krunal shah

  6. Priya says:

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

  7. GJ says:

    Gr8 work………Thanks a lot

  8. f says:

    very nice

  9. Surendra says:

    Thanx a lot. Very informative keep on

  10. Devendra says:

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

  11. khaled khunaifer says:

    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 …

  12. Anonymous says:

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

  13. harish says:

    good explanation

  14. mahidhar kundella says:

    very good explanation

  15. Kumar says:

    Detailed explanation. Thanks.

  16. chayan says:

    nice answer….
    thnx..

  17. Mayank says:

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

  18. Aarya says:

    Quite informative

  19. Margu says:

    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?

  20. ambrish says:

    Hi,

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

  21. Arivoli says:

    Very nice!!

  22. vidyasagar says:

    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.

  23. Waqas says:

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

  24. Anonymous says:

    Very Good Information..Thanks for sharing

  25. Anonymous says:

    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!

  26. Anu says:

    excellent!!

  27. Anonymous says:

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

    Thank you..

  28. chander kumar says:

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

  29. Nagabhooshanam says:

    Good Explanation…

    Thanks for it

  30. Juben says:

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

  31. nitinvavdiya says:

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

  32. Gopal says:

    Excellent write up.. A bunch of thanks….

  33. senthilkumar says:

    Nice explanation…

  34. Ranjana says:

    Good explaination…

  35. avi says:

    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

  36. lalit sharma says:

    i like your way of writing

  37. kirti says:

    very clear idea..thanks

  38. Jadumani says:

    Very Good explaination…….

  39. Ahmed says:

    Nice to know about static import.

  40. rahul says:

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

  41. Swaroop says:

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

  42. Abhishek says:

    Thank you for sharing.Clean explanation.

  43. san says:

    good explanation

  44. Anonymous says:

    Very useful Blog…………

  45. Anonymous says:

    good
    very usefull………..

  46. Anonymous says:

    useful blog…..

  47. Satya says:

    Thanx very much…….

  48. Anonymous says:

    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.

  49. abhiroop says:

    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.

  50. Anonymous says:

    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.

  51. Anonymous says:

    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

  52. Anonymous says:

    Hi Joe

    As usual, nice and simple explanation.

    Great Job.

    -Prasad

  53. Shiv Dubey says:

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

  54. naresh says:

    nice one, thanks

  55. Jatin Bansal says:

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

  56. Anonymous says:

    Thanks for explaining in very simple way :)

  57. Venkata Reddy Mylarapu says:

    Nice way of explaining about static import with examples to know when and where we have to use. :)

  58. Anonymous says:

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

  59. Manoj says:

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

  60. Bharat Sharma says:

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

    Cheers,
    Bharat Sharma

  61. shiva says:

    Very Good Explaination!!!!!!!!

  62. francis says:

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

  63. Babu says:

    Hi,

    Nice post
    Thanks you

  64. jayachandra says:

    Nice, Thanks

  65. Anonymous says:

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

  66. samba says:

    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

  67. Tanyasis Mwanik says:

    Awesome.

  68. pradip vora says:

    Good Explanation…………!!!

  69. Ravikumar says:

    Thanks for it… :)

  70. Anonymous says:

    that’s really helpful.

  71. Arbaz says:

    Excellent

  72. Sachin Janani says:

    Thank you Good Explanation

  73. rahamathali says:

    Is static import supports only java.lang.Math package

  74. Ali Reza says:

    this is good tutorial for me,
    tank you joe

  75. Ali Reza says:

    No , you can import static contents of any class.

  76. Ramabrahmam says:

    Awesome.. Thank u very much

  77. Rahul says:

    HI Joe,

    reading in all posts and sites the static clause as part of import statement, we may use it in code as below

    “static import something.*”

    This is syntactically wrong.

    I am not telling to correct your post, but feed in the thought to all those who read, its always

    ” import static ” in java.

    Thanks for explanation…

  78. hema says:

    i dont think so joe is wrong.this syntax is
    correct.
    import static java.lang.Math.PI;

    double j = PI*100;
    System.out.println(“printvalue”+j);
    it is working..

  79. Anonymous says:

    Thanks for simplied explanation

  80. Bhawesh says:

    This post is very enlightening.Thank you sir.

  81. Anonymous says:

    what is static memebers.can u plz briefly

  82. Nithin says:

    I did not get this:

    “the second option might be the only option, when you have either to use a private static member, or when the class is private !”

    How can we import private classes?? Never heard of that. Also I think we can not import private static members. Please give an example if I am wrong.

  83. Anonymous says:

    Static members are members of a class, not an object. They are always available, and only one copy of a static member exists for any given class. So, if you define a class as follows:

    public class MyClass {
    public static final String STATIC_CONSTANT = “somevalue”;
    }

    The variable ‘STATIC_CONSTANT’ is available through the class, but not through objects of the class. You would access it outside of the class by using:

    MyClass.STATIC_CONSTANT

    And, static values are accessible without an instance of the class having been created.

  84. Anonymous says:

    Those are two separate classes and should be within their own files.

    One file, which should be named MyConstant.java, is:

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

    The other file, which should be named MyMath.java, is:

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

    It works just fine.

  85. Samit Banerjee says:

    thnx joe! it is really useful for the programmer like me, expecting lot more so simple explanations from your end.

  86. Sunil says:

    Thanx joe.

  87. kanchi says:

    Thanks Joe, explained in a easier manner :)

  88. gaurav dasgupta says:

    Today during a discussion the Static import concept came up. I was curious and googled and see what I got this article at 2nd number in search list and this was so helpful and easy to understand. Thanks Joe.

  89. praveen says:

    Thanks for sharing valuable knowledge…

  90. simmant says:

    again you rock joe . I am big fan of your blog because you make very simple and easy approach to learn the most complected issues of java . Go ahead Joe and keep on posting new blogs. God Bless

  91. Anonymous says:

    very nice . explained beautifully thanks :)

  92. satyanarayana says:

    Nice and simple explanation

Comments are closed for this "Java Static Import" tutorial.