keywords that control the visibility and accessibility of classes, methods, constructors and variables. They are a core part of encapsulation, allowing you to hide sensitive data and restrict how other parts of a program interact with your code.
In Java, modifiers define the scope, behavior, and properties of classes, methods, and variables.
define how the members of a class, like variables, methods, and even the class itself, can be accessed from other parts of our program.
We divide modifiers into two groups:
Access Modifiers - controls the access level
Non-Access Modifiers - do not control access level, but provides other functionality
Non-access modifiers do not control visibility (like public or private), but instead add other features to classes, methods, and attributes.
The most commonly used non-access modifiers are final, static, and abstract.
Final:
If you don't want the ability to override existing attribute values, declare attributes as final.
Static:
A static method belongs to the class, not to any specific object. This means you can call it without creating an object of the class.
but it cannot use variables or methods that belong to an object.
Abstract:
An abstract method belongs to an abstract class, and it does not have a body. The body is provided by the subclass
Non-Access Modifiers List
For classes, you can use either final or abstract:
final :The class cannot be inherited by other classes .
abstract :The class cannot be used to create objects (To access an abstract class, it must be inherited from another class)
For attributes and methods, you can use the one of the following:
final :Attributes and methods cannot be overridden/modified
static :Attributes and methods belong to the class, not to objects. This means all objects share the same static attribute, and static methods can be called without creating objects.
abstract :Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from).
transient :Attributes and methods are skipped when serializing the object containing them
synchronized :Methods can only be accessed by one thread at a time
volatile :The value of an attribute is not cached thread-locally, and is always read from the "main memory"
Key Rules and Restrictions
Top-Level Classes: A top-level class can only be public or default. It cannot be declared private or protected.
public :The class is accessible by any other class
default :The class is only accessible by classes in the same package. This is used when you don't specify a modifier.
For attributes, methods and constructors, you can use the one of the following:
public: The code is accessible for all classes
private :The code is only accessible within the declared class
default :The code is only accessible in the same package. This is used when you don't specify a modifier.
protected :The code is accessible in the same package and subclasses.
Method Overriding: When overriding a method, you cannot reduce its visibility (e.g., you cannot override a public method with a protected one), but you can increase it.
Local Variables: Access modifiers cannot be applied to local variables inside methods; doing so will result in a compilation error.
Interfaces: By default, all methods in an interface are public and all fields are public static final
🔥 Types of Access Modifiers
Java has 4 access modifiers:
Modifier Accessible Within Class Same Package Different Package Subclass Different Package Non-Subclass
private ✅ ❌ ❌ ❌
default ✅ ✅ ❌ ❌
protected ✅ ✅ ✅ ❌
public ✅ ✅ ✅ ✅
When to Use Each Access Modifier in Real-World Projects
Private: The idea should be use as restrictive access as possible, so private should be used as much as possible.
Default (Package-Private): Often used in package-scoped utilities or helper classes.
Protected: Commonly used in inheritance-based designs like framework extensions.
Public: This is used for API endpoints, service classes, or utility methods shared across different parts of an application.
Top comments (0)