Java Packages

A Package can be stated that a grouping of related types (classes, interfaces, enumerations, and annotations ) providing access protection and namespace management.

Some of the existing packages in Java are:

  • java.io – classes for input , output functions are bundled in this package.
  • java.lang – bundles the fundamental classes.

Creating a Package

The package statement should be the first line in the source file. There can be only one package statement in each source file, and it applies to all types in the file.

To compile the Java programs with package statements, you have to use -d option as shown below.

$ javac -d Destination_folder file_name.java

folder with the given package name is created in the specified destination, and the compiled class files will be placed in that folder.

Example:

/* File name : Human.java */package Human;

interface Human {
   public void work();
   public void eat();
}

Now, let us implement the above interface in the same package Human.

package animals;

/* File name : MammalInt.java */
public class MammalInt implements Animal {

   public void eat() {
      System.out.println("Mammal work");
   }

   public void travel() {
      System.out.println("Mammal eat");
   } 

   public int noOfLegs() {
      return 0;
   }

   public static void main(String args[]) {
      MammalInt m = new MammalInt();
      m.eat();
      m.travel();
   }
}

Now compile the java files as shown below:

$ javac -d . Animal.java 
$ javac -d . MammalInt.java

You can execute the class file within the package and get the result as shown below.

Mammal work
Mammal eat

The import Keyword

If a class wants to use another class in the same package, the package name need not be used. Classes in the same package find each other without any special syntax. Example Here, a class named Oldschools is added to the payroll package that already contains Employee. The Oldschools can then refer to the Employee class without using the payroll prefix, as demonstrated by the following Oldschool class.

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

What happens 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 wild card (*). For example:

import payroll.*;

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

import payroll.Employee;

Set CLASSPATH System Variable

To display the current CLASSPATH variable, use the following commands in Windows and UNIX (Bourne shell):

  • In Windows → C:\> set CLASSPATH
  • In UNIX → % echo $CLASSPATH

To delete the current contents of the CLASSPATH variable, use:

  • In Windows → C:\> set CLASSPATH =
  • In UNIX → % unset CLASSPATH; export CLASSPATH

To set the CLASSPATH variable:

  • In Windows → set CLASSPATH = C:\users\jack\java\classes
  • In UNIX → % CLASSPATH = /home/jack/java/classes; export CLASSPATH
Related Post