Packages and Access Modifiers
Packages organise classes into folders and namespaces; access modifiers control who can use what.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
Packages are departments in a company (com.shop.orders, com.shop.billing). Access modifiers are door rules: public for everyone, protected for family, no modifier for the same department, private for the owner only.
- package
- Namespace matching a folder path.
- import
- Use a class from another package.
- package-private
- No modifier: visible within the same package.
Learning objectives#
- Organise classes into packages
- Import classes
- Apply the four access levels
- Follow standard project structure
Explanation#
package com.techiebabaji.shop.orders; must match the folder com/techiebabaji/shop/orders.
import brings in classes from other packages (java.lang is imported automatically).
Access levels: public (everywhere), protected (package + subclasses), package-private (same package, no keyword), private (same class).
Maven/Gradle use src/main/java and src/test/java.
Code example: Package structure#
// File: src/main/java/com/techiebabaji/shop/orders/Order.java
package com.techiebabaji.shop.orders;
import java.math.BigDecimal;
import java.util.UUID;
public class Order {
private final String id = UUID.randomUUID().toString(); // only this class
BigDecimal total = BigDecimal.ZERO; // package-private
protected String status = "NEW"; // package + subclasses
public String getId() { return id; } // everyone
}
// File: src/main/java/com/techiebabaji/shop/App.java
// package com.techiebabaji.shop;
// import com.techiebabaji.shop.orders.Order;
// Order o = new Order();
// o.getId(); ✓ public
// o.total; ✗ compile error: total is not public in Order
Code outside the orders package can call getId() but cannot touch total or id directly.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The package declaration matches the folder.
- Imports bring in BigDecimal and UUID.
- Each field shows a different access level.
- Other packages see only public members.
Real-world use case#
Large codebases split packages by feature (orders, billing, users) so internal details stay hidden.
Common mistakes#
The compiler or IDE reports errors. Keep them in sync.
Expose only what other packages need.
Best practices#
- Reverse-domain package names
- Package by feature
- Smallest access level that works
- Avoid wildcard imports
Remember this#
package = folder path.
public > protected > package-private > private.
Expose as little as possible.
Practice exercise#
Your task: Which access modifier lets only classes in the same package use a method?
Show solution
No modifier (package-private).
Mini quiz#
Which access level makes a member visible only inside its own class?
Want more? Practise all Java MCQs · Java interview questions
My notes#
Saved in this browser.