What is a package?

Packages are just a collection/group of classes, enums, interfaces, and other packages. The main purpose of packages is to reduce application complexity by organizing them in an appropriate way.

Why we need packages?

Just like we do in our computer system we arrange and organize different types of files in different directories so that we can find them later quickly. Similarly, while development we keep Java class and other resources in their respective package by their purpose. For example – 

  1. util – Package for all the utility classes.
  2. exception – Package for all the user-defined exception classes.
  3. screen – Package to hold all the UI screens.
  4. controller – Package to hold all the controllers.
  5. etc…

One of the major advantages of a package is, it can be used to avoid class name confliction, You can create two classes with the same name in different packages. Later we use package with the class name to identify them uniquely.

Types of packages?

There are two types of packages – 

  1. Built-in package
  2. User-defined package

Built-in package

These are the predefined packages available in Java, or you can say standard package provided as part of the Java Runtime Environment. Following are the examples of some predefined packages.

  1. java – A top-level package
  2. java.io – It contains classes to support input/output.
  3. java.util – It contains utility classes.
  4. java.applet – It contains applet related classes.
  5. java.net – It contains networking related classes.
  6. etc…

User-defined package

As the name says these are the user-defined or packages created by a programmer to group related classes.

How to create a package?

Java has a keyword “package” for declaring package statements in a class. The package statement must be written at the top of the outermost class as the first statement. Following is the syntax for declaring a package statement.

Syntax

package <package-name>;

Example

Follow below-shown steps to create a package ‘sample’.

  1. Create a directory ‘sample’.
  2. Now create a Java class ‘Example.java’ inside the directory ‘sample’.
  3. Now write the package statement package sample; at top of the Example class.
// Package statement
package sample;

public class Example {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }
}

Compile

There are multiple ways to compile and run a Java class with a package. Following is the simplest one – 

To compile and run a class with the package we need to follow a few more additional steps suppose if your directory structure is like:

Java Tutorial
——sample
————Example.java

Then navigate to the parent/root directory.

>> cd ..
Java Tutorial >> 

Now compile the class

Java Tutorial >> javac sample/Example.java

To run the class having main() method just remove ‘.java’

Java Tutorial >> java sample/Example
Hello World!

Nested or Subpackage

A package located inside another package is known as a subpackage. To create a subpackage we just need to manage package statement and directory structure. Suppose if you want to create another new package inside package ‘sample’ then simply create a new directory inside ‘sample’.

Syntax

package <package-name1>.<package-name2>.<package-nameN>;

Example

For example package, ‘util’ is a subpackage-

Java Tutorial
——sample
————util
——————Example.java

Place the Java class inside ‘util’ and write the package name separated by a dot (.), Like – 

// Subpackage statement
package sample.util;

public class Example {

    public static void main(String[] args) {

        System.out.println("Hello World!");

    }
}

Points to remember

  1. A class can have only a single package statement.
  2. A package statement should be the very first statement in class.
  3. Subpackages should be separated by a dot (.).
  4. It is always a good practice to place a class with a package statement to their respective directory.
  5. If no package name is specified, the class is in the default package (the current working directory).
  6. It is always good practice to write a package name in lowercase with an underscore (_) separator for multiple words.
  7. A Java package name follows almost all the Class naming conventions.