How To Import A Class In Java? – Step By Step Guide #2023

Table of Contents

import is a keyword in java that is used to include a class or a package in our program. We use import keyword with packages and classes. If there are 2 classes with the same name in different packages and we want to use both of them then import helps us to differentiate between them. import keyword is used to make the code more readable and understandable. import is present in java.lang package itself.

import java.lang.String;

Once a class is imported, you can reference it by its name without having to specify the package name. So in our example, we could create a String object like this:

String myString = new String(“Hello World”);

If you import multiple classes from the same package, you can use the import keyword followed by the class names, separated by commas. For example:

import java.lang.String, java.lang.Math;

You can also import all the classes in a package using the import keyword followed by the asterisk character (*). For example, to import all the classes in the java.util package:

import java.util.*;

 

 

No data found.