Access Modifiers in Java: A Complete Guide
Java offers four access modifiers that decide how classes, variables, methods, and constructors can be reached from other parts of your code. Knowing what each one does lets you build safer, better-encapsulated programs. The four are default, private, protected, and public.
Would you rather watch than read? A Hindi-language video walkthrough is available.
Watch the Video1. Default Access Specifier in Java
If you declare a class, variable, method, or constructor with no modifier at all, Java falls back to the default access level on its own. A default member is visible only within the package where it is declared and cannot be reached from another package. That places default above private in openness but below protected and public.
Default Class
A class with no modifier is a default class. It is usable anywhere inside its own package, yet code in a different package cannot see it. Trying to import and use it from another package triggers a compilation error.
Default Variable
A variable with no modifier is a default variable, reachable only within its package. Even when the enclosing class is public, a default field stays off-limits to classes in other packages, and the compiler reports that the field is not visible.
Default Method
A method with no modifier is a default method. It can be called from inside the same package but not from beyond it. Invoking such a method from another package yields a compilation error saying the method is not visible.
Default Constructor
A constructor with no modifier is a default constructor, invokable only inside the package where it is defined. Consequently, classes in other packages cannot create instances through that constructor, even if the class itself is public.
2. Private Access Specifier in Java
The private modifier, written with the private keyword, gives the tightest access level in Java. Private members are visible only within the class that declares them, which makes private the most restrictive modifier you can use.
Private Class
A top-level class cannot be private; Java allows only public or default at that level. Inner classes, by contrast, may be private, in which case they are readable only from inside their enclosing class.
Private Variables
Making a variable private hides it from every other class. Should another class try to read or change it, the compiler raises an error at compile time. This is a popular way to shield internal state.
Private Methods
A private method can be called only from within its own class. A handy example is a password-setting method: you might want the class itself to manage password changes while stopping outside classes from calling the method directly. Any external call ends in a compilation error.
Private Constructors
A private constructor prevents other classes from creating instances of the class. When an outside class tries to call it, the compiler reports that the constructor is not visible.
3. Protected Access Specifier in Java
The protected modifier relies on the protected keyword. Its scope spans the same package and also extends beyond it, but only via inheritance. It cannot be applied to a top-level class. Protected is more flexible than default and private alike.
Protected Class
As with private, protected cannot be applied to a top-level class; only public and default are permitted there. Inner classes can be declared protected and read from within their enclosing context.
Protected Variables
A protected variable is accessible inside its own package and, beyond the package, only through a subclass that inherits it. If an unrelated class in another package tries to access the variable, the compiler throws an error at compile time.
Keep Exploring
Access modifiers are tightly tied to packages and inheritance, so these related topics will sharpen your understanding.
- Discover how packages group related classes and govern visibility.
- See how inheritance lets subclasses reach protected members across packages.
Choosing the right modifier comes down to one question: who should be able to see this member? Keep fields private by default, open them up with default or protected only when a package or subclass genuinely needs access, and reserve public for the API you intend others to rely on.
