Packages & Access Modifiers
A package is a namespace that groups related classes, like folders for your code. Access modifiers — public, protected, the default (no keyword), and private — control which other code can see and use a class, field, or method. Together they keep large projects organised and let you expose a clean public surface while hiding internal details. Packages also prevent name clashes: two classes named Order can coexist in different packages.
Declaring a package
The package declaration is the first line of a source file, and the folder structure must mirror it.
// File: com/infoplanet/billing/Invoice.java
package com.infoplanet.billing; // matches the folder path
public class Invoice {
// ...
}
Package names are lowercase and usually follow a reversed-domain convention (com.company.module) to stay globally unique.
Importing from another package
To use a class from a different package, import it (or write its fully qualified name).
package com.infoplanet.app;
import com.infoplanet.billing.Invoice; // bring the class into scope
import java.util.List; // standard-library import
public class Main {
public static void main(String[] args) {
Invoice inv = new Invoice();
}
}
Classes in java.lang (like String, System, Math) are imported automatically — you never write import java.lang.String.
The four access modifiers
| Modifier | Same class | Same package | Subclass (other package) | Anywhere |
|---|---|---|---|---|
private | Yes | No | No | No |
| default (none) | Yes | Yes | No | No |
protected | Yes | Yes | Yes | No |
public | Yes | Yes | Yes | Yes |
public class Account {
private double balance; // only this class
String owner; // default: any class in the same package
protected String type; // package + subclasses elsewhere
public String id; // everyone
private void audit() { } // internal helper, hidden from callers
public double getBalance() { return balance; } // controlled access
}
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesChoosing the right modifier
The guiding principle is least privilege: expose as little as possible.
- Make fields
privateand providepublicmethods to read or change them — this is encapsulation. - Mark a method
publiconly when outside code genuinely needs it. - Use the default (package-private) level for classes and helpers meant to stay internal to a module.
- Use
protectedwhen a subclass — possibly in another package — legitimately needs access.
A small example across packages
// com/infoplanet/billing/Invoice.java
package com.infoplanet.billing;
public class Invoice {
private double amount; // hidden
public Invoice(double amount) { this.amount = amount; }
public double getAmount() { return amount; } // the public door in
}
// com/infoplanet/app/Report.java
package com.infoplanet.app;
import com.infoplanet.billing.Invoice;
public class Report {
public static void main(String[] args) {
var inv = new Invoice(1500);
System.out.println(inv.getAmount()); // 1500.0
// inv.amount -> would NOT compile: amount is private
}
}
Common mistakes
- Package declaration not matching the folder.
package com.x;requires the file to sit in acom/x/directory. - Making fields public. This leaks internal state and breaks encapsulation. Default to
private. - Forgetting the import. Using a class from another package without importing it (or fully qualifying it) fails to compile.
- Confusing default with public. No modifier means package-private, not public — a common surprise.
- Putting two public classes in one file. A
.javafile may contain only one top-levelpublicclass, and its name must match the file.
FAQ
What is a "module" in modern Java? Since JDK 9, modules (module-info.java) group packages and declare which are exported — a layer above packages for large applications. Packages and access modifiers are the foundation you learn first.
Can two classes have the same name? Yes, if they live in different packages. Their fully qualified names differ.
Keep going
Next, work with text using String Handling in Java, or revisit Interfaces & Abstract Classes. All tutorials live on the Java hub; for guided practice in Jalgaon, see the Java course.
Structure real-world Java projects at Infoplanet, Jalgaon. Join the waitlist for the Java course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in Java
Store many values of one type with Java arrays: declaring, initializing, indexing, iterating, multidimensional arrays, and the Arrays helper class.
Classes & Objects in Java
Define classes with fields and methods, create objects with new, use the this keyword, and understand instance vs static members in Java.
The Java Collections Framework
Use the Java Collections Framework — List, Set, and Map with ArrayList and HashMap — plus generics and iteration, to store flexible groups of objects.
