Package in Java: Complete Guide with Examples
A package in Java is a namespace that groups related classes, interfaces, and sub-packages together. Packages prevent naming conflicts, control access visibility, and make large codebases manageable.
In this guide, you will learn the two types of packages, how to declare and create them, how to access package classes in four different ways, and the best practices used in real Java projects.
What is a Package?
A package is a Java keyword used to group related classes and sub-packages together.
Key facts about packages:
- A package is declared with the
packagekeyword at the top of a Java file. - Packages remove conflicts between file names — two classes named
MyClasscan live in different packages. - A package can hold a group of public classes, interfaces, and enums.
- Sub-packages are not automatically imported — they must be imported explicitly.
Types of Packages
There are two types of packages in Java:
- Built-in packages (or predefined packages)
- User-defined packages
1. Built-in Packages
Java provides many built-in packages for performing different tasks. Common examples include:
java.lang— core classes (String,Object,System)java.util— collections, date, randomjava.io— input/output streamsjava.net— networking classesjava.awt— abstract window toolkitjavax.swing— GUI components
Note: The java.lang package is imported
automatically by every Java program. You never need to write
import java.lang.*;.
2. User-Defined Packages
You can also create your own packages to organize your classes and sub-packages. This is the standard practice in any real Java project.
How to declare a package?
Use the package keyword followed by the package name:
package packagename;
How to declare a sub-package?
Use dot notation to nest sub-packages:
package packagename.subpackagename;
Where to declare a package?
The package declaration must be the first statement in a Java file.
package packagename;
public class A {
}
Can we write anything above the package declaration?
Only comments can appear above the package declaration. No other statements are allowed.
// This is a comment
package packagename;
public class A {
}
How to Save a File
Save the file with the same name as the public class:
ClassName.java
How to Create a Package
Compile the Java file with the -d flag to place the class file in the correct package folder structure:
javac -d . filename.java
Here -d denotes the destination and . represents the current
directory.
Create a package in a different location
javac -d C:// filename.java
Run the packaged class
Use the fully qualified name to run the class:
java packagename.ClassName
Multiple Classes in One Package
There are two ways to bind multiple classes in a single package:
Way 1 — All classes in a single file
Write all classes in one .java file and declare the package at the top. Only
one class can be public; the rest must be default (package-private).
package a.b.c;
public class A {
}
class B {
}
class C {
}
Note: In this approach, only one class can be public — the one whose name matches the file name. All other classes must be default.
Way 2 — Separate files for each class
Create a separate .java file for each class, make each one public, and
declare the same package in all files.
// A.java
package a.b.c;
public class A {
}
// B.java
package a.b.c;
public class B {
}
// C.java
package a.b.c;
public class C {
}
Note: In this approach, all classes can be public.
4 Ways to Access Package Classes
There are four ways to access classes from a package:
-
Fully qualified name — no import needed, just use the full path:
java.util.ArrayList list = new java.util.ArrayList(); -
Import a single class — the most common approach:
import java.util.ArrayList; ArrayList list = new ArrayList(); -
Import a group of classes — using wildcard
*:import java.util.*; ArrayList list = new ArrayList(); HashMap map = new HashMap(); -
Static import — imports static members so you can use them without class prefix:
import static java.lang.Math.PI; import static java.lang.Math.sqrt; double area = PI * radius * radius; double root = sqrt(16);
Frequently Asked Questions
What is a package in Java?
A package in Java is a namespace used to group related classes, interfaces, and
sub-packages together. It prevents naming conflicts, controls access, and makes code
organization easier. Packages are declared with the package keyword at the
top of a Java file.
What is the difference between a package and a sub-package?
A package is a top-level container (like java.util). A sub-package is a
nested package inside another package (like java.util.concurrent).
Sub-packages are not automatically imported — you must import them
explicitly.
What are the 4 ways to access classes from a package in Java?
1) Use the fully qualified name (java.util.ArrayList).
2) Import a single class (import java.util.ArrayList).
3) Import all classes in a package using wildcard (import java.util.*).
4) Use static import (import static java.lang.Math.PI).
Why is java.lang imported automatically in Java?
java.lang contains the most fundamental Java classes like
Object, String, System, and Math.
Since every Java program uses these classes, the Java compiler imports
java.lang automatically to reduce boilerplate import statements.
How do I create a package in Java?
Declare the package at the top of your .java file
(package com.example;), then compile with the -d flag:
javac -d . MyClass.java. The -d flag tells the compiler to
place the .class file in the appropriate folder structure matching the
package name.
Conclusion
Packages are a fundamental organizational tool in Java. They prevent naming conflicts, control access, and make large projects manageable. You have learned:
- What a package is and why it exists
- The difference between built-in and user-defined packages
- How to declare, create, and run packages
- How to organize multiple classes in one package
- Four ways to access package classes — including
static import
The next step is to study JAR files, which package your compiled classes into a single distributable archive.
Related topics: JAR File in Java, Java Class and Object, Access Modifiers in Java, Java Import Statements, Java Package Naming Conventions, Java Classpath