Codectionary / Developer documentation / Java

Access Modifiers

Access modifiers control the visibility of classes, fields, and methods from other parts of a program. Java has four levels: public (accessible from anywhere), protected (accessible within the same package and by subclasses), default/package-private (accessible only within the same package, used when no modifier is written), and private (accessible only within the declaring class itself).

Syntax

public | protected | (default) | private

Examples

The Four Access Levels

Demonstrating each modifier on fields within a class.

public class AccessDemo {
    public int publicField = 1;        // accessible from anywhere
    protected int protectedField = 2;   // accessible in package + subclasses
    int defaultField = 3;                // accessible only within the same package
    private int privateField = 4;        // accessible only within this class

    private void showAll() {
        // all four are accessible from inside the class itself
        System.out.println(publicField + protectedField + defaultField + privateField);
    }
}

Private Fields with Public Accessors

The standard encapsulation pattern: hide data, expose controlled access through methods.

public class Account {
    private double balance;  // hidden from outside access

    public Account(double initialBalance) {
        this.balance = initialBalance;
    }

    public double getBalance() {  // controlled read access
        return balance;
    }

    public void deposit(double amount) {  // controlled write access, with validation
        if (amount > 0) {
            balance += amount;
        }
    }
}

Account acc = new Account(100);
// acc.balance = -500;  // Error - balance is private, can't be accessed directly
acc.deposit(50);
System.out.println(acc.getBalance());  // 150.0

protected and Inheritance

protected members are accessible to subclasses even if they're in a different package.

public class Vehicle {
    protected int speed;

    protected void accelerate() {
        speed += 10;
    }
}

public class Car extends Vehicle {
    public void drive() {
        accelerate();  // accessible because Car extends Vehicle
        System.out.println("Speed: " + speed);
    }
}

Car car = new Car();
car.drive();  // Speed: 10

Best practices

  • Default to private for fields, exposing access only through public getter/setter methods when genuinely needed (encapsulation)
  • Use protected specifically when subclasses need access, and public only for the intentional external API of a class
  • Avoid the temptation to make everything public "just in case" - it removes the safety encapsulation is meant to provide
  • Keep helper methods that support a public method private, so implementation details stay hidden and changeable

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

Classes
Classes in Java are blueprints for creating objects, defining their properties (fields) and behaviors (methods). Java is a strictly object-oriented language where everything is encapsulated within classes. A class serves as a template that specifies what data an object will store and what operations it can perform.
Interfaces
An interface defines a contract of methods that implementing classes must provide, without specifying how those methods work internally. Unlike a class, an interface cannot be instantiated directly - it only declares what a class can do, not how. A class uses the 'implements' keyword to fulfill an interface's contract, and a single class can implement multiple interfaces, which is Java's way of achieving a form of multiple inheritance.
Abstract Classes
An abstract class is a class that cannot be instantiated directly and may contain both fully implemented methods and abstract methods (declared without a body, which subclasses must implement). Abstract classes sit between interfaces and regular classes: like an interface, they define a contract; like a regular class, they can hold state (fields) and provide shared, already-implemented behavior.
Constructors
A constructor is a special method that runs automatically when an object is created with 'new', typically used to initialize the object's fields. A constructor shares its name with the class and has no return type, not even void. Java supports constructor overloading, letting a class offer multiple ways to construct an object, and constructor chaining with this(...), where one constructor calls another in the same class.