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:

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:

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:


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:

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!

Related Article:

Why Learn to Program why java

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button