Key Points
- Access modifiers in Java manage the visibility and accessibility of fields, classes, and methods, with four types: public, private, protected, and default (package-private).
- Public access allows the highest level of visibility, while private access is the most restrictive, only accessible within the same class.
- Protected access allows classes in the same package and subclasses to access the class, method, or field, while package-private access is limited to classes within the same package.
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?
Packages are a useful way of organizing interfaces and classes in Java. 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:
src/
└── com/
└── example/
└── myPackage/
├── MyClass1.java
└── MyClass2.java
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
Applying an access modifier to a class looks something like this:
[access_modifier] class ExampleClass {
}
Note that, for the most part, these rules will apply to interfaces as well.
Public
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.
Private
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.
Protected
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.

©History-Computer.com
Access Modifiers in Java: Methods
Using access modifiers on methods inside classes looks something like this:
[access_modifier] [return_type] exampleMethod() {
}
Public
Similar to public classes, methods declared 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.
Private
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
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;
Public
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.
Private
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.
Protected
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.
The image featured at the top of this post is ©Wright Studio/Shutterstock.com.