Java Packages


Packages are utilized in Java information to put off naming differences, to control admission, to make probing/locating and practice of classes, interfaces, details, and annotations easier, etc.

A Package can be identified as an alliance of connected types (classes, interfaces, enumerations, and annotations) given that access protection and namespace organization.

Some of the obtainable packages in Java are:

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

Programmers can describe there have packages to pack groups of classes/interfaces, etc. It is a high-quality do to group connected classes applied by you so that a programmer can effortlessly decide that the classes, lines, details, and gloss are related.

Because the package creates, a novel namespace there will not be any name clashes with names in other encloses. With packages, it is easier to offer admission control and it is easier to place the related classes.

Creating a Package

Whereas creating a enclose, you should decide a name for the enclose and contain a package report along with that forename at the top of each source file that holds the classes, borders, listings, and annotation types that you desire to incorporate in the package.

The package declaration should be the primary column in the basis file. There can be only one enclose report in all source files, and it applies to entire types in the file.

If a package announcement is not utilized then the class, lines, listings, and explanation types will be positioned in the existing default package.

To accumulate the Java agenda with package accounts, you have to old alternative as revealed below.

javac -d Destination_folder file_name.java

Then a folder with the known package first name is created for the particular purpose, and the compiled class files will be put in that folder.


Let us appear at a case that generates package-identified animals. It is good quality carry out to use names of wrap-ups with lower case letters to shun any conflicts with the names of classes and lines.

The following package example contains interface named animals

/* File name : Animal.java */
package animals;

interface Animal {
   public void eat();
   public void travel();

Now, let us implement the above interface in the same package animals:

package animals;
/* File name : MammalInt.java */

public class MammalInt implements Animal {

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

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

   public int noOfLegs() {
      return 0;

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

Now compile the java files as shown below:

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

Now a package/folder with the name animals will be created in the current directory and these class files will be placed in it as shown below.

You can perform the class file inside the package and acquire the consequence as exposed below.

Mammal eats
Mammal travels

The import Keyword

If a class desires to employ another group in a similar package, the package name requires not to be utilized. Classes in a similar package discover every other with no particular syntax.


Here, a group named Boss is put into the payroll enclose that previously holds Employee. The Boss can then submit to the Employee class with no by the payroll prefix, as displayed by the subsequent Boss class.

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

What occurs if the Employee class is not in the payroll package? The Boss class has to then use one of the subsequent techniques for submitting to a class in a diverse package.

  • The fully qualified name of the class can be used. For example:
  • 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:

Note − A class file container holds any number of import reports. The import statements must emerge after the package report and ahead of the class assertion.

The Directory Structure of Packages

Two main results happen when a class is put in a package −

  • The first name of the package turns into a division of the name of the class, as we immediately discussed in the preceding section.
  • The name of the wrap-up must competition the directory arrangement where the matching bytecode resides.

Here is an effortless way of organizing your files in Java −

Put the source code for a class, line, enumeration, or gloss type in a text file whose name is the easy name of the type and whose addition is .java.

For example

// File Name :  Car.java
package vehicle;

public class Car {
   // Class implementation.   

Now, put the source file in a directory whose name reflects the name of the package to which the class belongs


Now, the qualified class name and pathname would be as follows −

  • Class name → vehicle.Car
  • Path name → vehicle\Car.java (in windows)

In general, a company uses its reversed Internet domain name for its package names.

Example − A company's Internet domain name is apple.com, then all its package names would start with com.apple. Each component of the package name corresponds to a subdirectory.

Example − The company had a com.apple.computers package that contained a Dell.java source file, it would be contained in a series of subdirectories like this −


At the time of collection, the compiler generates a diverse output file for each group, interface, and list defined in it. The base forename of the output file is the forename of the type, and its annex is .class.

For example

// File Name: Dell.java
package com.apple.computers;

public class Dell {

class Ups {

Now, compile this file as follows using -d option:

$javac -d . Dell.java

The files will be compiled as follows:


You can import all the classes or interfaces defined in \com\apple\computers\ as follows

import com.apple.computers.*;

Similar to the .java source files, the compiled .class files ought to be in a succession of directories that reproduce the package's first name. However, the path to the .class files never has to be similar to the path to the .java source files. You can position your source and class directories disjointedly, as



By doing this, it is probable to give the right of entry to the classes list to other programmers without no illuminating your sources. You also necessitate administering source and class files in this method so that the compiler and the Java Virtual Machine (JVM) can discover all the types your program uses.

The full path to the classes directory, \classes, is called the classpath and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the classpath.

Say \classes is the class path, and the package name is com.apple.computers, then the compiler and JVM will look for .class files in \classes\com\apple\computers.

A classpath may incorporate numerous paths. Multiple paths should be divided by a semicolon (Windows) or colon (Unix). By default, the compiler and the JVM investigate the present directory and the JAR file including the Java platform classes so that these directories are repeatedly in the classpath.

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

Here at Intellinuts, we have created a complete Java tutorial for Beginners to get started in Java.