Feature of Import Class In Java With Examples
Feature of import class:
Import class:- There are thousands of classes in the Java class library. These classes are again grouped into packages. Some of the most important Classes are in the java.lang package. These classes include, for example: the aforementioned String class. The special feature of the java.lang package consists in the fact that the classes contained therein can be used; without explicitly naming the package name each time.
However, for all other packages, the package name must be specified in the following. The following rows show how the variable r of type java.util.Random. This variable can be set to a random Reference object. In the next line, the random object is created with new, where the entire package name is specified. Finally, the Method nextInt of the random class two random numbers between 0 and 99 generated and output with println:
1 2 3 4 |
java .util .Random r; r = new java .util.Random (); System.out.println ("A random number: " + r.nextInt (100)); System.out.println ("Another random number: " + r. nextInt (100)); |
The constant writing of the package name is tedious in the long run. Therefore, Java provides that you create classes that you use in a code file. can import in advance:
1 2 3 4 5 |
import java .util .Random; ... Random r; r = new Random(); System.out.println ("A random number: " + r.nextInt (100)); |
Strictly speaking, nothing is imported. You say more the compiler that you use every time you use the class name Random in the Use code, in reality java.util.Random mean.
A variant of import package.class is import package.*. That’s what makes everyone classes defined in this package without you package name.
As a final example, the following listing shows how to handle the LocalDate and DateTimeFormatter classes. These classes are available in the packages java.time or java.time.format. The corresponding import statements come before class. Code execution begins in the main method. The variable is now declared there. There, with now creates a LocalDate object that specifies the current date. Under Using a DateTimeFormatter object, the date becomes a People converted into easily readable form.
Example: how to use import in java programming:
1 2 3 4 5 6 7 8 9 10 11 12 |
import java .time. LocalDate ; import java.time .format.DateTimeFormatter ; public class HelloClass { public static void main (string [] args ) { // Program execution starts here LocalDate now = LocalDate.now (); DateTimeFormatter myformatter = DateTimeFormatter.ofPattern ( "EEEE , d.MMMM yyyy "); System.out.println (" Today is " + myformatter.format(now) + "."); } } |
OutPut:
1 |
Today is Friday, december 11, 2020. |
For reasons of space, I usually do without in the further course of this book on it, the always same code public class xyz and public static void main. The shortened listing then looks like this:
1 2 3 4 5 6 7 |
import java .time.LocalDate ; import java.time.format.DateTimeFormatter ; ... LocalDate now = LocalDate.now (); DateTimeFormatter myformatter = DateTimeFormatter.ofPattern ( "EEEE, d. MMMM yyyy "); System.out.println (" Today is " + myformatter.format(now) + "."); |
But that doesn’t change the fact that you still have the class and must define the main method for the program to be syntactic is correct. The structure of each Java program thus looks like the Hello-World Program Out!