Static import is a feature that provides the facility to access static members of a class directly without using a class name.
Java Static import Example
package com.w3schools.display; public class Display { public static void displayText(String text){ System.out.println(text); } }
package test; import static com.w3schools.display.Display.*; public class Test { public static void main(String args[]){ displayText("Hello W3schools360.com!"); } }
Output
Hello W3schools360.com!
Advantages of static import
It reduces the code by removing the need for a class name to access static data members.
Disadvantages of static import
- Static import reduces the code readability because of class name absence.
- If two classes have static data members with the same name and both classes are statically imported. Then Java will throw a compile-time error.
Import vs static import in Java
import | static import |
|
|