Java 5 introduced the static import feature, which enables Java developers to directly access any static member of a class without the need to qualify it with the class name.
Accessing static members of a class frequently can reduce the amount of code that needs to be written.
Excessive utilization of the static import functionality can lead to decreased readability and maintainability of the program.
Simple Example of static import
import static java.lang.System.*;
class StaticImportExample{
public static void main(String args[]){
out.println("Hello");//Now no need of System.out
out.println("Java");
}
}
Output:Hello
Java
What is the difference between import and static import?
Import in Java enables programmers to utilize classes from a package without specifying the package each time. On the other hand, static import permits accessing static members of a class without specifying the class. Import grants access to classes and interfaces, while static import grants access to static members of a class.