Home  >  Article  >  Java  >  Java Static Import

Java Static Import

WBOY
WBOYOriginal
2024-08-30 15:57:38545browse

Java Static Import is a mechanism that helps in using a static member of any class directly. The static items in a class can be referenced easily in the module without another qualification required. There is no need for any class name or object, and these static objects can be used directly. It is a good way of increasing code readability and enhancing the quality of the code. It is not advisable to use static import unless it is necessary.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Working of Static Import in Java

The static import helps us in using the static members of the class directly. It makes all class members accessible, which in turn helps in using the members of that class without mentioning the class name repeatedly. The below code is an example of static import. You are not required to use the module of System class that is System.out. Instead, you can directly refer out.println in the code, and that program will run successfully as we have imported the System class statically.

import static java.lang.System.*;
public class StaticImportInstance{
public static void main(String args[]){
out.println("Hi");//Now no need of System.out
out.println("We are using Static Import");
}
}

Examples of Java Static Import

Let us see some examples which use static import functionality.

Example #1

Importing the inbuilt Java Math class statically.

Code:

import static java.lang.Math.PI;
import static java.lang.Math.pow;
public class HelloStaticWorld {
public static void main(String[] args) {
System.out.println("Hello, Let us get introduced to the Static Import World!");
System.out.println("Let us consider  a circle with a diameter of 5 cm, it has");
System.out.println("a circumference of " + (Math.PI * 5) + " cm");
System.out.println("and the area of circle will be" + (Math.PI * Math.pow(2.5, 2)) + " sq. cm");
}
}

Output:

Java Static Import

The above program is importing the Math class. Going further ahead, it is also importing the PI and POW methods statically. Once these are used statically, we will not need to create any further references or objects in our program. We can easily use the imported classes. We have made use of Math.PI to use the value of PI and found the circumference of the circle. Also, we have found the area of the circle by again using the PI and POW methods which help us in finding the area of the circle. If you check the screenshot, you will see the circumference and area of a circle are displayed.

Example #2

Importing the inbuilt Java Integer class

Code:

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.MIN_VALUE;
public class StaticImportInteger {
public static void main(String args[]) {
//Finding Max and Min without static import
System.out.println("We will find maximum value of an integer variable in Java without using" + "static import : " + Integer.MAX_VALUE);
System.out.println("We will find minimum value of an integer variable in Java without using" +
"static import : " + Integer.MIN_VALUE);
//Finding Max and Min without static import
System.out.println("We will find maximum value of an integer variable in Java with  using" +
"static import : " + MAX_VALUE);
System.out.println("We will find minimum value of an integer variable in Java with  using" +
"static import : " + MIN_VALUE);
}
}

Output:

Java Static Import

The above example is importing the Integer class statically. This means that we can use the methods in this class directly and use them in the program without any further reference. We are finding the maximum number in integers and the minimum number of integers first where we do not use the static import and use the normal function. Here we will make use of the class name and then use the function name. If you see the example, we have used Integer.Min_Value and Integer.Max_Value. In the code that follows, we make use of the static import. Here, if you check, you will see that we have directly used MAX_VALUE and MIN_VALUE methods. The result will be the same for both. This tells us that there are two ways in which we can make use of different Java functions.

Example #3

Ambiguity while using static import

Code:

import static java.lang.Integer.*;
import static java.lang.Long.*;
public class HelloAmbiguity{
public static void main(String[] args) {
System.out.println(MAX_VALUE);
}
}

Output:

Java Static Import

There can be a condition as above when the static members of the same class are being imported. When this happens, the compiler is confused as to which member to choose. As a result, it faces ambiguity when it comes to members of the same class. The above example has the lang class being imported twice. The different members it uses are Integer and Long. When the main function is called, the compiler is confused upon which member to choose between Integer and Long. It will not be able to reckon which member should it use in the absence of a specific class name. Hence it throws an error, as you can see in the above screenshot.

Advantages

There are numerous advantages of using the static method. To name a few, you can see the below:

  • You can access many classes that are not dependent on any of their instances together and used them separately.
  • By importing methods that are supposed to do related work, we can group these stateless utilities and, in turn, make them consistent by which it can also use parameters and return types directly.
  • The methods which are imported by making use of the static import are better at the performance. They do not require any explicit null check. They are faster.
  • When we use the static method, we make the code more readable. This is because there are a smaller number of class names included.
  • It helps in making reusability an easier approach. You can reuse the code, and the classes just need to be imported statically.

Conclusion

The static import method is an easy way of using the members of any class without creating any references for that class. When we import the class, we can directly use these methods. The static import enables the user to have a code that needs a smaller number of keystrokes and also a code that will be more efficient. We can use static import, but it is advisable to use it only when the requirement says so.

The above is the detailed content of Java Static Import. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java PopNext article:Java Pop