Skip to content

Java Access Modifiers Tutorial

Java access modifiers control the visibility of classes, methods, and variables. They define what code can access a class, method, or variable.

1. Types of Access Modifiers

ModifierClassPackageSubclassWorld (anywhere)
private
default (no modifier)
protected
public

Notes

  • private: Accessible only within the same class.
  • default (package-private): Accessible within the same package. No keyword is used.
  • protected: Accessible in the same package and in subclasses (even if in different packages).
  • public: Accessible from anywhere.

2. Access Modifiers for Classes

  • Top-level classes: public or default only.
  • Inner classes: Can be private, default, protected, or public.
java
// Default access
class DefaultClass {
}

// Public access
public class PublicClass {
}

3. Access Modifiers for Methods and Variables

java
public class AccessExample {
    private int privateVar = 1;      // Only in this class
    int defaultVar = 2;              // Only in package
    protected int protectedVar = 3;   // Package + subclasses
    public int publicVar = 4;         // Everywhere

    private void privateMethod() {
        System.out.println("private");
    }

    void defaultMethod() {
        System.out.println("default");
    }

    protected void protectedMethod() {
        System.out.println("protected");
    }

    public void publicMethod() {
        System.out.println("public");
    }
}

4. Example Usage

java
package package1;

public class TestAccess {
    public static void main(String[] args) {
        AccessExample obj = new AccessExample();

        // System.out.println(obj.privateVar); // ❌ Error
        System.out.println(obj.defaultVar);   // ✅ Accessible within package
        System.out.println(obj.protectedVar); // ✅ Accessible within package
        System.out.println(obj.publicVar);    // ✅ Accessible everywhere

        obj.defaultMethod();    // ✅
        obj.protectedMethod();  // ✅
        obj.publicMethod();     // ✅
        // obj.privateMethod(); // ❌ Error
    }
}
  • Private members cannot be accessed outside the class.
  • Default members are accessible only within the same package.
  • Protected members are accessible in the same package or subclasses in different packages.
  • Public members are accessible everywhere.

5. Access Modifiers for Constructors

java
class Example {
    private Example() { // Only accessible within the class
        System.out.println("Private constructor");
    }
}
  • Using a private constructor is common in Singleton patterns or utility classes.

6. Best Practices

  1. Keep members as restrictive as possible (default to private).
  2. Use getter/setter methods for controlled access.
  3. Only expose what is necessary (public) to reduce coupling.
java
public class EncapsulationExample {
    private int age; // Hidden

    public int getAge() { return age; }       // Controlled access
    public void setAge(int age) { this.age = age; }
}

Quick Recap:

  • private → only within the class
  • default → only within the package
  • protected → package + subclasses
  • public → everywhere

© 2023-2025 Maduranga Kannangara. Feel free to use or share this content. Attribution is appreciated but not required.