Example of Static Import in JavaJava programming is used by many users worldwide. It provides numerous packages to solve different problems. To use the Java packages in our programs, import keyword is used. In this section, we will discuss about static import in Java. Java import KeywordMost of the Java programs starts with the statements having import keyword. It is similar to the preprocessor directives used in C or C++ programming. The import in Java is a keyword that allows the programmer to access packages available in Java. It is used to import a package, sub-package, a class, an interface or enum in the Java program. Syntax: The following Java program demonstrates the use of import statement. ExampleImport.java Output: Demonstrating use of import statement in Java Tue Jul 20 18:42:43 UTC 2021 In the above code, the java.util package is used in the program and it is included using the import statement at the start of the program. Java Static ImportThe Java static import was introduced in JDK version 1.5. With the help of static import, the static variables and methods of imported classes can be accessed. We don't need to specify the class name or object name to access the methods or variables. Using a static import statement saves time because the programmer isn't required to use the class name or object name again and again. Syntax: Program before using the static import statementThe following program shows the use of the methods of the Math class without using the static import statement. ExampleImport1.java Output: Square root of 9 = 3 Result of 2 * pi = 6.283185307179586 In the above Java code, the static methods sqrt(), round() and variable PI of the Math class are accessed directly by using the class name. Program after using the static import statementThe following program shows the use of methods of the Math class with the use of the static import statement. ExampleStaticImport.java Output: Square root of 9 = 3 Result of 2 * pi = 6.283185307179586 In the above Java code, the Math class is imported using static import. The methods sqrt(), round() and variable PI of Math class are directly accessed without using the class name. How static import statement is used?1. Importing all data members using an asterisk (*): When the programmer wants to use all the static data members of the class, it can be done in the following way. ExmapleStaticImport.java Output: Minimum value of an Integer: -2147483648 Maximum value of an Integer: 2147483647 In the above Java code, data members of the Integer class are imported using an asterisk symbol that denotes all the static members that can be used in the program without specifying the class name. 2. mporting static variables by names: The situation where the Java program only requires the specific static variable to be used. It can be imported in the following manner. ExampleStaticImport2.java Output: 2 * PI = 6.283185307179586 In the above Java code, the static variable PI of the Math class is only used. 3. Importing static methods by names: The situation where the Java program only requires the specific static method to be used. It can be imported in the following manner. ExampleStaticImport3.java Output: Square root of a = 4.0 In the above Java code, the sqrt() method of the Math class is only used. Advantages of static import
Disadvantages of static import
Next TopicHill Cipher Program in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India