Java Static Import - Java Tutorial

Java Static Import

BLUF: Mastering Java Static Import is a key requirement for any Java developer. This lesson breaks down the object-oriented principles and syntax required to use this concept in real-world applications.
Write Once, Run Anywhere Tip: Java Static Import

Java's versatility is unmatched. Learn how Java Static Import fits into the Java ecosystem and improves your code structure in the tutorial below.

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

Example

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");



 } 

}
Example

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.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience