Java Packages

A package in java is a group of similar classes, interfaces, enumerations and sub-packages. It is also used to manage class files in hierarchical file systems to prevent naming conflicts, control access of class and to keep related classes and interfaces together in same directly.

Java programming language provides lots of inbuilt packages for common tasks which we can directly use in our programs like java.lang, java.io, java.util, java.applet, java.swing etc.


Advantages of Package in Java
  • As related classes are grouped together in a package, it becomes easy to categorize and search a files.

  • Package provides access protection.

  • Packages avoid namespace collisions. A package creates a new namespace, hence there won't be any namespace conflicts with two classes of same name in different packages.

  • Packages support re-usability of software components. Anyone can import a shared package and include it in his class file and reuse existing classes.

Types of Java Package
  • User Define Packages : These are the packages created by user for his specific requirements. These packages are not part of the standard JDK.

  • Built-in Packages : These are the ready use use, in-built packages provided by JDK like java.lang, java.util etc.

Creating a Package in Java

"package" keyword is used to create a new package. To create a package we have to include a "package" statement specifying the name of the package at top of every interface, classes and enumeration that you want to include in this package.

  • package statement must be the first statement in any source file.
  • Only one package statement is allowed per source file.

However, package statement is optional if we don't specify package in a source file then it will be part of the default package.


Syntax of package statement

package package_name;

Naming Conventions of Package
  • Package name should be in lowercase characters.

  • To ensure that the package name is unique, generally the company url is used starting in backword.

  • For Example : package com.companyname.application.module;

Compiling a Package in Java

The java compiler will place the class file of a class in the directory that corresponds to the package declaration of class.

For Example:
Class Employee is specified package is tcc.JavaPrograms. Then on compilation, Employee.class file will be placed in folder ./tcc/JavaPrograms.

If you want to specify the destination folder location to store the generated .class files then use –d option when compiling with the javac compiler.

javac -d destinationDirectory javaFileName 
For Example:
javac -d /tcc/JavaPrograms Employee.java

Accessing Java Packages from Other Package

If a class wants to access any other class in same package then it can access it directly. Classes in same package can access each other without any package import or special syntax. However, if a class wants to access a class in different package then we have to do one of the following :

  • Using Fully Qualified Name : Here we don't have to import the desired, but we have to use the fully qualified name of the class every time we want to access it.
    For Example:
    tcc.JavaPrograms.Employee emp = new tcc.JavaPrograms.Employee();
    
    NOTE : Using fully qualified names of classes in different package is considered a bad programming practice.

  • By importing the class we want to use : Here, import keyword is used to import classes and packages into your java program file. Once imported your class access imported class by simply using it's name.
    For Example:
    import tcc.JavaPrograms.Employee;
    class Demo {
       Employee e;
    }

  • By importing all the classes of a package : Instead of selectively importing required classes one by one, we can import all the classes and interfaces of a package to our java source file by using package.*
    For Example :
    import tcc.JavaPrograms.*;
    class Demo {
       Employee e;
    }
    Above import statement will make all the classes and interfaces of tcc.JavaPrograms package accessible in current java source file.