How to use classes in other package in Java


You can understand it using an example where a Boss class is defined in payroll package.

package payroll;
public class Boss {
   public void payEmployee(Employee e) {
      e.mailCheck();
   }
}

if the Employee class 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.

  • The fully qualified name of the class can be used. For example −
payroll.Employee
  • The package can be imported using the import keyword and the wildcard (*). For example −
import payroll.*;
  • The class itself can be imported using the import keyword. For example −
import payroll.Employee;


Updated on: 04-Feb-2020

444 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements