Packages
Packages are an encapsulation mechanism that group related classes and interfaces into a single unit.
What is a package?
A package groups related types (classes/interfaces) for modularity, naming, and access control. Physically, a package maps to a directory; logically, it is a namespace.
Examples
- java.sql — classes and interfaces related to database operations.
- java.io — classes and interfaces for file I/O operations.
Main advantages
- Resolve name conflicts (unique identification).
- Improve modularity of the application.
- Improve maintainability.
- Provide security for components (access control).
Naming convention
Use the internet domain name in reverse, followed by module/submodule/class. Package names are written in lowercase. Example:
com.example.shop.orders.Invoice
- com.example — reversed domain
- shop — module
- orders — submodule
- Invoice — class
Code example
Two classes in two different packages. Project layout:
src/
└── com/acme/
├── util/Greeter.java
└── app/Main.java
src/com/acme/util/Greeter.java
package com.acme.util;
public class Greeter {
public String greet(String name) {
return "Hello, " + name;
}
}
src/com/acme/app/Main.java
package com.acme.app;
import com.acme.util.Greeter;
public class Main {
public static void main(String[] args) {
System.out.println(new Greeter().greet("Java"));
}
}
Compiling & class file placement
Without -d, the generated .class file is placed in the current working directory, not in a package folder:
javac Greeter.java
This works for a quick test, but the folder structure that the JVM expects (matching the package) is not created, so running it by its fully qualified name will fail.
javac -d
javac -d out src/com/acme/util/Greeter.java src/com/acme/app/Main.java
-d specifies the destination root. javac creates the package folder structure under it automatically:
com.acme.util becomes the folder path com/acme/util.-d .places the package folders in the current directory.- You can point -d at any destination — e.g.
javac -d F:\classes ...on Windows orjavac -d /some/path ...on Linux/macOS. - On modern javac (JDK 9+), a missing destination directory is created automatically. Older versions reported a "directory not found" error.
Execution
Run using the fully qualified class name, with the classpath pointing at the destination root:
java -cp out com.acme.app.Main
Rules & Conclusion
- At most one package statement is allowed per source file.
- If used, the package declaration must be the first non-comment statement in the file.
- Valid Java source file structure (when package is present):
Access modifiers (summary)
Recommended: make data members private, expose methods as public when needed.
| Context | public | private | protected | default |
|---|---|---|---|---|
| Within same class | Allowed | Allowed | Allowed | Allowed |
| Child class (same package) | Allowed | Not allowed | Allowed | Allowed |
| Non-child class (same package) | Allowed | Not allowed | Allowed | Allowed |
| Child class (outside package) | Allowed | Not allowed | Allowed (use child reference) | Not allowed |
| Non-child class (outside package) | Allowed | Not allowed | Not allowed | Not allowed |
Access order (least → most permissive): private < default < protected < public
0 Comments