Java · Object-Oriented Java

Packages and Access Modifiers

Packages organise classes into folders and namespaces; access modifiers control who can use what.

Intermediate 5 min Lesson 16 of 31Examples target Java 21 LTS (they also run on Java 25).
0/31 in Java
On this page · 5 min · Intermediate

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#

JavaPackage structure Illustrative
// 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
Expected output
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#

  1. The package declaration matches the folder.
  2. Imports bring in BigDecimal and UUID.
  3. Each field shows a different access level.
  4. 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#

Package name not matching the folder

The compiler or IDE reports errors. Keep them in sync.

Making everything public

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
Text Illustrative
No modifier (package-private).

Mini quiz#

Packages and Access Modifiers quizMultiple choice Question 1 of 1

Which access level makes a member visible only inside its own class?

Score so far: 0/0

Want more? Practise all Java MCQs · Java interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.