Packages In Java

Java Packages — Complete Guide

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.
java.io File FileReader InputStream java.sql Connection Statement ResultSet
Related types grouped under one package name.

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:

out/ com/ acme/ util/ app/ Greeter.class Main.class
Package name 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 or javac -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):
1. package statement package com.acme.app; 2. import statements import com.acme.util.Greeter; 3. class / interface / enum declaration
Required order inside a source file.

Access modifiers (summary)

Recommended: make data members private, expose methods as public when needed.

Context public private protected default
Within same class AllowedAllowedAllowedAllowed
Child class (same package) AllowedNot allowedAllowedAllowed
Non-child class (same package) AllowedNot allowedAllowedAllowed
Child class (outside package) AllowedNot allowedAllowed (use child reference)Not allowed
Non-child class (outside package) AllowedNot allowedNot allowedNot allowed

Access order (least → most permissive): private < default < protected < public

0 Comments