Demonstrate Static Import in Java


Static import is a feature that was introduced in Java version 5 and above. It means that the fields and methods in a class can be used in the code without specifying their class if they are defined as public static.

A program that demonstrates this in Java is given as follows:

Example

 Live Demo

import static java.lang.System.*;
public class Demo {
   public static void main(String args[]) {
      out.println("Demonstration of Static Import in Java");
      int a = 10;
      out.println("Value of a is: " + a);
   }
}

Output

Demonstration of Static Import in Java
Value of a is: 10

Now let us understand the above program.

The System class is not required in System.out.println() as static import is used for the java.lang package. So there is no need to specify the class System to use the out member field of the System class and the println method. A code snippet which demonstrates this is as follows:

out.println("Demonstration of Static Import in Java");
int a = 10;
out.println("Value of a is: " + a);
raja
Published on 15-Feb-2019 14:57:43
Advertisements