Skip to content

Intro to Packages

As our Java applications become more sophisticated and complex, we use more and more classes.

  • There might be many developers working on different parts of the same project, leading to the possibility of class name collisions.

name collision

Two developers choosing the same name for completely different classes.

Java packages allow us to organize our classes and minimize name collisions by providing namespaces for classes and other Java types.

Because filesystem directories define namespaces, Java packages are based on them.

  • You can have a folder package1 containing a Hello.java, and another folder package2 containing a completely different Hello.java

Each class can specify the package it belongs to.

File package1/Hello.java:

package package1;

public class Hello { /*...*/ }

File package2/Hello.java:

package package2;

public class Hello { /*...*/ }

The "default" Package

If your class has no package statement then it belongs to the default package, also known as the unnamed package.

  • Once you've advanced beyond the "Hello World" stage, using the default package is considered a bad practice.

Practice Exercise

A class which has no package declaration is part of the unnamed package, so every Java class lives in a package.


Prev -- Up -- Next