Understanding Access Modifiers in Java, With Examples

Shutterstock.com / Wright Studio

Access Modifiers in Java are keywords that manage the visibility and accessibility of fields, classes, and methods. In this article, we will go over all four of them and explain how they can be used.

But first, let s review what packages are.

What are Packages?

In Java, packages are a helpful tool for class and interface organization. They are often named according to reversed domain names. For example, if you wanted two Java classes MyClass1 and MyClass2 to be in the same package, you could put this at the top of each of them:

package com.example.myPackage;

And the directory structure would look something like:


Depending on what access modifiers are used, the package that classes are in can greatly affect accessibility throughout the program.

Access Modifiers in Java: Classes

The process of adding an access modifier to a class looks like this:

[access_modifier] class ExampleClass {

Note that, for the most part, these rules will apply tointerfacesas well.


When a class is declared public, it can be accessed from any other class or package, making it the highest level of visibility. It is important to note that there can only be one public class per Java file, and the name of this class must match the name of the file.


A private class can only be accessed inside the same enclosing class. This is typically used for inner classes that assist in the functionality of the outer one. This is the most restrictive modifier, and subclasses or classes in the same package cannot access a class with it.


When a class is protected, this means that it can be accessed by classes in the same package, as well as subclasses that are not necessarily in the same package.

Default (Package-Private)

A class is considered package-private when no access modifier is specified. If a class is package-private, it can only be accessed by classes in the same package. This is especially useful when it is not necessary for implementation details to be accessed within a package.

access modifiers in java
This example shows 3 different types of access modifiers in Java: public, private, and protected.


Access Modifiers in Java: Methods

Using access modifiers on methods inside classes looks something like this:

[access_modifier] [return_type] exampleMethod() {



Similar to public classes,methodsdeclared as public can be accessed anywhere within the program. Public methods are typically the ones that interface with the bulk of the class s functionality.


A private method is only accessible within the same class. They are usually created to abstract away implementation details that are not necessary for outside classes.


Protected methods can be accessed by classes in the same package and subclasses, provided that the class containing the method is accessible.

Default (Package-Private)

A package-private method can only be accessed by classes in the same package, just as when this modifier is used for a class. This is done when certain functionalities only need to be used by other classes in the package.

Access Modifiers in Java: Fields

When you are applying an access modifier to a field inside a class, it looks like:

[access_modifier] [data_type] exampleField;


As expected, public fields can be accessed by any class within a program, as long as the enclosing class is accessible. You should try to avoid public fields, as it is better practice in Java to use getters and setters that promote encapsulation.


A private field can only be accessed in the same class. The majority of the fields in a class will likely be private, as it is not necessary for other classes to know implementation details.


If a field is protected, then classes in the same package or subclasses can access it. This is most commonly used when you explicitly want fields to be inherited when a subclass is created.

Default (Package-Private)

Although not as common, package-private fields work similar to package-private classes and methods, allowing them to be accessed by classes within the same package only.

Wrapping Up

When you are starting out programming in Java, you don t need to worry too much about what access modifiers you are applying, as long as they don t cause problems. However, with larger projects, encapsulation and abstraction become more and more important, and you must use them to their fullest extent.

Learn fully what each modifier does, and your personal and professional projects will be better organized and maintainable. Now, let s dive into some frequently asked questions.

Leave a Comment