What is static import in java



In this tutorial we will learn What is static import with example and programs in java.





Contents of page >
  • What is static import in java?
  • What Java docs says about static import
  • Why to use Static import in java? What is advantage of using Static import in java ?
  • When not to use Static import in java? Is there any disadvantage associated with use of Static import in java ?
  • Static import was introduced in which version of java ?


  • Program / Example 1 - How to use static import in java
  • Program / Example 2 - How to use static import in java, in this example we will use more than one static imports in java.
  • Program / Example 3 - How to use static import to import static variables in java
  • Program / Example 4 - How to use static import to import static class in java
  • Program / Example 5 - How to use static import to import static members of java.lang.Math
  • Program / Example 6 - How to use static import to import static members of java.lang.Math

What is static import in java?
Static import can be used to access only static members of other class in java.
Static import can be used to access static variables, static methods and static classes of other class in java.
We could import multiple static members using wildcard (*) in java.


What Java docs says about static import - ‘Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names’


Now lets see what Java docs say about static import in detail -
‘ So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.’

Why to use Static import in java? What is advantage of using Static import in java ?
Static import makes your code more readable and understandable.


When not to use Static import in java? Is there any disadvantage associated with use of Static import in java ?
Though Static import makes your code more readable and understandable but excessive use of static imports may make your java code dirty and unreadable.


Static import was introduced in which version of java ?
Static import was introduced in java 5.

Program / Example 1 - How to use static import in java


First, create OperationClass
package staticImportPkg1;
public class OperationClass {
   public static void add(int a, int b) {
          System.out.println("addition result = "+ (a + b));
   }
}


Now, create main class which will use static import to use static method of OperationClass in java.
package staticImportPkg2;
import static staticImportPkg1.OperationClass.add; //static import in java
class StaticImportExample{
   public static void main(String[] args) {
          add(3, 2);
   }
}
/*OUTPUT
addition result = 5
*/
So, above StaticImportExample class accessed add method of OperationClass using static import in java.

Program where static import in not used in java >
Now, let’s modify above program and see how our main class (StaticImportExample class) would have accessed add method of OperationClass without using static import in java.
package staticImportPkg2;
import staticImportPkg1.OperationClass; //This is not a static import in java
class StaticImportExample{
   public static void main(String[] args) {
          OperationClass.add(3, 2);
   }
}
/*OUTPUT
addition result = 5
*/
We imported OperationClass and accessed add method of OperationClass using OperationClass.add(3, 2) in java.



Program / Example 2 - How to use static import in java, in this example we will use more than one static imports in java.


First, create OperationClass
package staticImportPkg3;
public class OperationClass {
   public static void add(int a, int b) {
          System.out.println("addition result = "+ (a + b));
   }
   public static void subtract(int a, int b) {
          System.out.println("subtraction result = "+ (a - b));
   }
}


Now, create main class which will use more than one static import to use static method of OperationClass in java.
package staticImportPkg4;
import static staticImportPkg3.OperationClass.add;      //static import in java
import static staticImportPkg3.OperationClass.subtract; //static import in java

class StaticImportExample{
   public static void main(String[] args) {
          add(3, 2);
          subtract(3, 2);
   }
}
/*OUTPUT
addition result = 5
subtraction result = 1
*/
So, above StaticImportExample class accessed add and subtract methods of OperationClass using static import in java.

Now, let’s create program we could use one static import to import multiple static members in java >
Now, let’s modify above program and see how main class (StaticImportExample class) would have accessed add and subtract methods of OperationClass by using one static import rather than using multiple static imports in java.
We could import multiple static members using wildcard (*) in java.
package staticImportPkg4;
import static staticImportPkg3.OperationClass.*; //Import multiple static members

class StaticImportExample{
   public static void main(String[] args) {
          add(3, 2);
          subtract(3, 2);
   }
}
/*OUTPUT
addition result = 5
subtraction result = 1
*/
So, in the above program we imported multiple static members using wildcard (*) in java.



Program / Example 3 - How to use static import to import static variables in java


First, create OperationClass
package staticImportPkg5;
public class OperationClass {
   public static int i = 3;
}


Now, create main class which will use static import to use static variable of OperationClass in java.
package staticImportPkg6;
import static staticImportPkg5.OperationClass.i; //static import in java
class StaticImportExample{
   public static void main(String[] args) {
          System.out.println(i);
   }
}
/*OUTPUT
3
*/
So, above StaticImportExample class accessed static variable i of OperationClass using static import in java.



Program / Example 4 - How to use static import to import static class in java


First, create OperationClass
package staticImportPkg7;
public class OperationClass {
   public static class MyClass{
          public static void m(){
                 System.out.println("In OperationClass, static MyClass, method m()");
          }
   }
}


Now, create main class which will use static import to use static class of OperationClass in java.
package staticImportPkg8;
import static staticImportPkg7.OperationClass.MyClass;
class StaticImportExample{
   public static void main(String[] args) {
          MyClass.m();
   }
}
/*OUTPUT
In OperationClass, static MyClass, method m()
*/
So, above StaticImportExample class accessed static class MyClass of OperationClass using static import in java.


Program / Example 5 - How to use static import to import static members of java.lang.Math
package staticImport;
import static java.lang.Math.*;
public class StaticImportExample {
   public static void main(String args[]) {
          System.out.println(sqrt(9));
          System.out.println(incrementExact(9));
          System.out.println(decrementExact(9));
   }
}
/*OUTPUT
3.0
10
8
*/
So, above StaticImportExample class accessed static members of java.lang.Math using static import in java. We imported multiple static members using wildcard (*).

Above program without static import in java.
package staticImport;
public class StaticImportExample {
   public static void main(String args[]) {
          System.out.println(Math.sqrt(9));
          System.out.println(Math.incrementExact(9));
          System.out.println(Math.decrementExact(9));
   }
}
/*OUTPUT
3.0
10
8
*/



Program / Example 6 - How to use static import to import static members of java.lang.Math
package staticImport;
import static java.lang.System.out;
public class StaticImportExample {
   public static void main(String args[]) {
          out.println("static import example in java");
   }
}
/*OUTPUT
static import example in java
*/
So, above StaticImportExample class accessed static members of java.lang.System using static import in java.

Above program without static import in java.
package staticImport;
public class StaticImportExample {
   public static void main(String args[]) {
          System.out.println("static import example in java");
   }
}
/*OUTPUT
static import example in java
*/



So in this tutorial we learned what is static import with example and programs in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

eEdit
Must read for you :