Packages in Java

Package in java as the name suggests is a pack(group) of classes, interfaces and other packages. Packages are used in Java in order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.

Packages are used for:

  • Preventing naming conflicts. For example there can be two classes with name Teacher in two packages, college.staff.icse.Teacher and college.staff.cbse.Teacher
  • Making searching/locating and usage of classes, interfaces, enumerations and annotations easier.
  • Providing access control: protected and default have package level access control. A protected member is accessible by classes in the same package and its subclasses. A default member (without any access specifier) is accessible by classes in the same package only.
  • Packages can be considered as data encapsulation (or data-hiding).

Package in java can be categorized in two form, built-in package and user-defined package. All we need to do is put related classes into packages. After that we can simply write a import a class from existing packages and use it in our program.

Example of built-in packages in java –

built in packages in java

Package names and directory structure are closely related. For example if a package name is college.staff.cbse, then there are three directories, collegestaff and cbse such that cbse is present in staff and staff is present college.

Subpackages: Packages that are inside another package are the subpackages. These are not imported by default, they have to imported explicitly. If you import a package, all the classes and interface of that package will be imported excluding the classes and interfaces of the subpackages. Hence, you need to import the subpackage as well.

Simple example of java package

The package keyword is used to create a package in java.

//save as Simple.java  
package mypack;  
public class Simple{  
 public static void main(String args[]){  
    System.out.println("Welcome to package");  
   }  
}
How to compile java package

If you are not using any IDE, you need to follow the syntax given below:

javac -d Destination_folder file_name.java

For example

javac -d Destination_folder Simple.java
How to run java package program

You need to use fully qualified name e.g. mypack.Simple etc to run the class.

To Compile: javac -d . Simple.java
To Run: java mypack.Simple

Output: Welcome to package

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.

How to access package from another package?

There are three ways to access the package from outside the package.

  • import package.*;
  • import package.classname;
  • fully qualified name.
1) Using packagename.*

If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. The import keyword is used to make the classes and interface of another package accessible to the current package.

Example of package that import the packagename.*

//save by A.java  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello");}  
}
//save by B.java  
package mypack;  
import pack.*;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}
Output:Hello
2) Using packagename.classname

If you import package.classname then only declared class of this package will be accessible.

Example of package by import package.classname

//save by A.java  
  
package pack;  
public class A{  
  public void msg(){System.out.println("Hello");}  
}
//save by B.java  
package mypack;  
import pack.A;  
  
class B{  
  public static void main(String args[]){  
   A obj = new A();  
   obj.msg();  
  }  
}
Output:Hello
Important points:
    1. Every class is part of some package.
    2. If no package is specified, the classes in the file goes into a special unnamed package (the same unnamed package for all files).
    3. All classes/interfaces in a file are part of the same package. Multiple files can specify the same package name.
    4. If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name).
    5. We can access public classes in another (named) package using: package-name.class-name

3 thoughts on “Packages in Java

    • May 13, 2018 at 10:25 pm
      Permalink

      Thank you very much, Please do share the site with your friends and contacts too 🙂

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *