Access Modifiers and Access Specifiers in Java

Access Modifiers in Java: A Complete Guide

Java offers four access modifiers that determine how visible classes, variables, methods, and constructors are. These are default, private, protected, and public. Mastering them is key to building secure, well-organized Java applications.

Diagram showing Java access modifiers
Java access modifiers overview

1. Default Access Specifier

If you don't declare any access modifier, Java applies a default access level. In that case, the member is reachable only inside its own package and is hidden from other packages. This level is looser than private but tighter than protected and public.

Default Class

A class declared without a modifier is known as a default class. It is usable only within its own package; trying to use it from a different package triggers a compilation error.

package packageOne;
class ClassOne {
    void display() {
        System.out.println("This class has default access modifier");
    }
}

package packageTwo;
import packageOne.ClassOne;
public class ClassTwo {
    public static void main(String[] args) {
        System.out.println("We can't import ClassOne because it is not visible outside the package");
        System.out.println("This class will throw an error at compilation time");
    }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: at packageTwo.ClassTwo.main(ClassTwo.java:5)

Default Variable

A variable with no modifier is a default variable, and it can be reached only inside its package. Reaching it from another package leads to a compilation error.

package packageOne;
public class ClassOne {
    int a = 5;
    String b = "Hi";
}

package packageTwo;
import packageOne.ClassOne;
public class ClassTwo {
    public static void main(String[] args) {
        System.out.println("We can access ClassOne because it is public");
        ClassOne obj = new ClassOne();
        System.out.println(obj.a);
        System.out.println(obj.b);
    }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field ClassOne.a is not visible, The field ClassOne.b is not visible at packageTwo.ClassTwo.main(ClassTwo.java:10)

Default Method

A method declared without a modifier is a default method, available only within its package. Invoking it from another package produces a compilation error.

package packageOne;
public class ClassOne {
    void display() {
        System.out.println("This is default method of class");
    }
}

package packageTwo;
import packageOne.ClassOne;
public class ClassTwo {
    public static void main(String[] args) {
        System.out.println("We can access ClassOne because it is public");
        ClassOne obj = new ClassOne();
        obj.display();
    }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method display() from the type ClassOne is not visible at packageTwo.ClassTwo.main(ClassTwo.java:10)

Default Constructor

A constructor with no modifier is a default constructor, and it can be called only inside its package. Even when the class is public, you can't instantiate it from another package through a default constructor.

package packageOne;
public class ClassOne {
    ClassOne() {
        System.out.println("ok");
    }
}

package packageTwo;
import packageOne.ClassOne;
public class ClassTwo {
    public static void main(String[] args) {
        System.out.println("We can access ClassOne because it is public");
        ClassOne obj = new ClassOne();
    }
}

Note: A default constructor cannot be used to create objects outside its package, no matter whether the class itself is public.

2. Private Access Specifier

The private modifier limits access to the class that contains the member. It is the strictest modifier Java offers, and private members can be used only inside the same class.

Private Class

Top-level classes can't be marked private—only public and default are permitted. Inner classes, however, may be private and are reachable from within the enclosing class.

Private Variables

Private variables use the private keyword and are hidden from outside the class. Trying to read them from another class results in a compilation error.

public class ClassOne {
    private int a = 5;
    private String b = "Hello";
}

public class ClassTwo {
    public static void main(String[] args) {
        ClassOne obj = new ClassOne();
        System.out.println(obj.a);
        System.out.println(obj.b);
    }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field ClassOne.a is not visible, The field ClassOne.b is not visible at ClassTwo.main(ClassTwo.java:6)

Private Method

Private methods are declared with the private keyword and cannot be called from outside the class. A method that sets a user's password, for instance, can be private to block outside changes.

public class ClassOne {
    private String password;
    private void setPassword(String password) {
        this.password = password;
    }
}

public class ClassTwo {
    public static void main(String[] args) {
        ClassOne obj = new ClassOne();
        obj.setPassword("NewOne");
    }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method setPassword(String) from the type ClassOne is not visible at ClassTwo.main(ClassTwo.java:6)

Private Constructor

Private constructors use the private keyword and can't be invoked from outside the class. Trying to instantiate the class from another class causes a compilation error.

public class ClassOne {
    private ClassOne() {
        System.out.println("Private constructor");
    }
}

public class ClassTwo {
    public static void main(String[] args) {
        ClassOne obj = new ClassOne();
    }
}

Output: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor ClassOne() is not visible at ClassTwo.main(ClassTwo.java:5)

3. Protected Access Specifier

The protected modifier grants access within the same package and also outside it through inheritance. It can't be applied to top-level classes. It is looser than default and private but stricter than public.

Protected Class

As with private, top-level classes can't be protected—only public and default are allowed. Inner classes may be protected and are reachable from subclasses.

Protected Variables

Protected variables can be used inside the package and outside it through inheritance. Trying to reach them from a non-subclass in another package produces a compilation error.

package packageOne;
public class ClassOne {
    protected int a = 5;
    protected String b = "Hello";
}

package packageTwo;
import packageOne.ClassOne;
public class ClassTwo extends ClassOne {
    public static void main(String[] args) {
        ClassTwo obj = new ClassTwo();
        System.out.print

Explore More Java Topics

Expand your Java skills with these related tutorials:

Connect with Javagoal

Got questions? Contact us through chat or follow us on social media.

Chat NowFacebookVideo available in Hindi
Author avatar
Author
YouTube logo
YouTube
Instagram logo
Instagram

Choosing the right access modifier is a core part of encapsulation in Java. Start with the most restrictive level that works, then widen it only when another class or package genuinely needs access. This keeps your API small, prevents accidental changes, and makes your code easier to maintain.