Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between import and static import?

What real (i.e. practical) difference between a normal import statment and static import statement?

import static java.lang.System.*;    
class StaticImportExample{  
  public static void main(String args[]){  

   out.println("Hello");
   out.println("Java");  

 }   
}  

import java.lang.System.*;    
class StaticImportExample{  
  public static void main(String args[]){  

   System.out.println("Hello"); 
   System.out.println("Java");  

 }   
}  
like image 823
Shakthi Vel Avatar asked Feb 12 '15 11:02

Shakthi Vel


People also ask

What do you mean by static import?

Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static , to be used in Java code without specifying the class in which the field has been defined.

What are the two types of import?

There are two basic types of import: Industrial and consumer goods. Intermediate goods and services.

When should you import static?

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.

What is the difference between import and package?

As discussed above the package keyword is used to group certain classes and interface under one package and, the import keyword is used include/use the classes and interface from a package in the current program.


2 Answers

From java 5 onwards static import was introduced. Practically "import static" is used to reduce the number of keystrokes, which means that you need not to write the class name for a static member that you want to use.

As in your example, with import static java.lang.System.* you only need to write out.println("Hello"); whereas normally you have to write System.out.println("Hello"); i.e we have to write the class name(System) every time we need to call the static member(out) of it.

like image 74
venkatesh sharma Avatar answered Sep 18 '22 08:09

venkatesh sharma


In addition to @venkatesh's answer, it is worth pointing out the javadoc documentation about when should static import be used?

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.

like image 37
Simon Avatar answered Sep 22 '22 08:09

Simon