Styrow.dev
Question 236 of 247
Java Topic: General medium

Explain the import keyword.

Question

Explain the import keyword.

Answer

If a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other without any special syntax.

Example:

Here, a class named Boss is added to the payroll package that already contains Employee. The Boss can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Boss class.

package payroll;

public class Boss

{

public void payEmployee(Employee e)

{

e.mailCheck();

}

}

What happens if Boss is not in the payroll package? The Boss class must then use one of the following techniques for referring to a class in a different package:

1.The fully qualified name of the class can be used. For example:

payroll.Employee

2.The package can be imported using the import keyword and the wild card (*). For example:

import payroll.*;

3.The class itself can be imported using the import keyword. For example:

import payroll.Employee;