Once you understand why Java packages exist, the next step is learning how to create one. Creating a package is straightforward: declare the package at the beginning of the Java source file and organize the source file according to that package structure.
Although the syntax is simple, package creation becomes especially important when an application contains many classes. A good package structure makes a project easier to navigate, maintain, test, and extend.
Why Create Your Own Package?
Java already provides many built-in packages, but real applications usually need custom packages for their own classes. Creating packages allows developers to group classes according to their responsibilities and application domain.
| Reason | How It Helps |
|---|---|
| Organization | Keeps related classes together. |
| Namespace | Reduces conflicts between classes with the same name. |
| Access control | Supports package-level access to members and types. |
| Maintainability | Makes large projects easier to understand and modify. |
Basic Package Creation
Suppose we want to create a package named com.example.student. We can declare it at the beginning of a Java source file.
package com.example.student;
public class Student {
public void display() {
System.out.println("Student information");
}
}
The package statement tells the Java compiler that the Student class belongs to com.example.student.
Important: The package declaration should appear before the class declaration and normally before any import declarations. A source file can have only one package declaration.
Creating the Package Directory
In a conventional Java project, the package name is reflected in the directory structure. For com.example.student, the corresponding directories are commonly:
com/
└── example/
└── student/
The Student.java file can then be placed inside the student directory.
com/
└── example/
└── student/
└── Student.java
In modern Java projects, build tools usually manage this structure for you. You normally create the package through your IDE or follow the project's standard source directory layout rather than manually creating directories every time.
Creating a Package in an IDE
Most Java IDEs provide a direct option for creating a package. The exact menu names can differ, but the general process is similar.
- Create or open a Java project.
- Locate the project's source directory.
- Create a new package.
- Give the package a meaningful name such as com.example.service.
- Create Java classes inside the package.
The IDE normally creates the required directory structure and inserts the package declaration automatically.
Creating Multiple Packages
A project can contain many packages. For example, consider a simple student management application:
com.example.student.model com.example.student.service com.example.student.repository com.example.student.controller
Each package can contain classes related to a particular responsibility.
com.example.student.model
Student.java
Course.java
com.example.student.service
StudentService.java
CourseService.java
com.example.student.repository
StudentRepository.java
com.example.student.controller
StudentController.java
This approach prevents the project from becoming a single crowded collection of unrelated classes.
Creating a Package from the Command Line
Packages can also be created and compiled from the command line. Consider this source file:
package com.example.demo;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello from a package");
}
}
The source file can be compiled so that Java creates the appropriate package directory structure for the generated class file.
javac -d . Hello.java
The -d option specifies where the compiler should place generated class files. The compiler uses the package declaration to create the required directory structure.
After compilation, the resulting structure can look like this:
.
└── com/
└── example/
└── demo/
└── Hello.class
Running a Packaged Class
When running a packaged class from the command line, use its fully qualified class name rather than only the simple class name.
java com.example.demo.Hello
The Java runtime uses the package name to locate the class within the classpath.
Remember: If a class belongs to com.example.demo, its fully qualified name is com.example.demo.Hello, not simply Hello.
Creating a Package with Two Classes
A package can contain multiple related classes. For example, we can place Student and Course in the same package.
package com.example.school;
public class Student {
public void showStudent() {
System.out.println("Student class");
}
}
package com.example.school;
public class Course {
public void showCourse() {
System.out.println("Course class");
}
}
Both classes now belong to com.example.school. Classes in the same package can work together naturally, including access to package-private members when the access rules permit it.
Package Creation Does Not Mean Data Storage
A common beginner misunderstanding is thinking that a package stores objects or application data. It does not. A package is a naming and organizational mechanism for Java types.
Objects are created at runtime and stored in memory according to Java's runtime behavior. The package simply identifies where a class logically belongs and forms part of its qualified name.
Common Mistakes
- Writing the package declaration after the class declaration.
- Using inconsistent package names across source files.
- Running a packaged class using only its simple class name from the command line.
- Creating unnecessarily complicated package hierarchies.
- Assuming that a package is merely a physical folder and ignoring its namespace and access-control role.
Best Practices
- Use lowercase package names by convention.
- Choose package names that clearly communicate their purpose.
- Keep related classes together.
- Avoid putting unrelated classes into the same package.
- Follow the package structure already established by the project or organization.
- Use meaningful package hierarchies instead of creating deep structures without a practical reason.
Interview Insight
An interviewer may ask, "How do you create a package in Java?" A concise answer is: declare the package using the package keyword at the beginning of the source file, follow the appropriate directory structure, and compile the source so that the resulting classes are placed according to their package hierarchy.
Interview Tip: Do not say that a package is simply a folder. A folder is a physical organization mechanism, while a Java package is a namespace that also participates in access control. The directory structure normally mirrors the package name to help Java locate compiled classes.
Quick Revision
| Topic | Key Point |
|---|---|
| Package declaration | Uses the package keyword at the beginning of the source file. |
| Package structure | Usually corresponds to directories such as com/example/demo. |
| Multiple classes | Related classes can belong to the same package. |
| Compiler | The -d option can create the package directory structure for compiled classes. |
| Running | Use the fully qualified class name when running a packaged class. |
| Best practice | Use meaningful, lowercase, consistently structured package names. |
Creating a package is a small step syntactically, but it is an important step toward professional Java application design. Once classes are organized into meaningful packages, Java needs a mechanism for using those classes from other packages. That is where the import statement becomes essential.
