Java static import allows direct access to static members of a class without using the class name.
In this chapter, you will learn about Java static import, how it works, and its advantages and disadvantages.
Static import is a feature introduced in Java 5 that allows programmers to access static members (variables and methods) of a class directly, without qualifying them with the class name.
The static import reduces code length when static members are used frequently.
To use static import, we use the import static keyword followed by the class name and the static member.
Syntax:
It has the following syntax:
If you want to import all static members of a class, you can use the wildcard *:
Using static import allows you to access static methods and variables directly without using the class name.
The following examples demonstrate how to use static import in Java.
In this example, we import a specific static method and use it directly without the class name.
Output:
4.0
In this example, we import all static members of a class using * and use them directly.
Output:
5.0 8.0
The import statement allows programmers to access classes and interfaces from a package without using the full package name.
The static import allows direct access to static members (variables and methods) of a class without using the class name.
In short, import is used for classes and interfaces, while static import is used for static members of a class.
We request you to subscribe our newsletter for upcoming updates.