ContactUs :

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

What is static import?

If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name.


import java.lang.Math;

//inside class
double test = Math.PI * 5;
We can do the same thing by importing the static method or variable only and then use it in the class as if it belongs to it.


import static java.lang.Math.PI;

//no need to refer class now
double test = PI * 5;
Use of static import can cause confusion, so it’s better to avoid it. Overuse of static import can make your program unreadable and unmaintainable.
Related Posts Plugin for WordPress, Blogger...
Flag Counter