Modifiers in Java

Gaurav Shah
5 min readJun 22, 2024

--

Introduction

In Java, modifiers define the scope, behavior, and properties of classes, methods, and variables. These modifiers are divided into two main categories access modifiers and non-access modifiers. In this blog, we’ll delve into the modifiers available in Java and their usage.

Modifiers in Java

Access Modifier:

Access modifiers control the accessibility of a class or an interface, including its members (methods and variables), by other classes and interfaces within the same or separate packages. By using the appropriate access modifiers, you can limit access to your class or interface and its members.
i. A top-level class can only be defined using public or default access modifiers.
ii. Access modifiers can be applied to classes, interfaces, and their members (instance and class variables and methods).
iii. Local variables and method parameters can’t be defined using access modifiers. An attempt to do so will prevent the code from compiling.

If a Java class, interface, method, or variable isn’t defined using an explicit access modifier, it is said to be defined using the default access, also called package access.

Java has four access levels:

1. public: This is the least restrictive access modifier. Classes and interfaces defined using the public access modifier are accessible across all packages, from derived to unrelated classes.

Scope of public access modifier

2. protected: The members of a class defined using the protected access modifier are accessible to
Classes and interfaces defined in the same package
All derived classes, even if they’re defined in separate packages

Scope of protected access modifier

3. default: The members of a class defined without using any explicit access modifier are defined with package accessibility (also called default accessibility). The members with package access are only accessible to classes and interfaces defined in the same package. The default access is also referred to as package-private.

Scope of default access modifier

4. private: The private access modifier is the most restrictive. The members of a class defined using the private access modifier are accessible only to themselves.

Scope of private access modifier
Java entities and the access modifiers that can be applied to them

Non-access modifier:

Non-access modifiers change the default behavior of a Java class and its members.

  1. abstract modifier: When added to the definition of a class, interface, or method, the abstract modifier changes its default behavior.
    Abstract class: When the abstract keyword is prefixed to the definition of a concrete class, it changes it to an abstract class, even if the class doesn’t define any abstract methods.
    An abstract class can’t be instantiated.
    An abstract class may or may not define an abstract method. However, a concrete class can’t define an abstract method.
    Abstract interface: An interface is an abstract entity by default. Thus, adding the keyword abstract to the definition of an interface is redundant.
    Abstract Method: An abstract method doesn’t have a body. Usually, an abstract method is implemented by a derived class.
    Abstract Variables: None of the different types of variables (instance, static, local, and method parameters) can be defined as abstract.
  2. final modifier: The keyword final can be used to declare a class, variable, or method.
    It can’t be used with the declaration of an interface.
    Final class: A final marked class can’t be extended by another class.
    Final interface: An interface can’t be marked as final. An interface is abstract by default and marking it with final will prevent your interface from compiling.
    Final variable: A final variable can’t be reassigned to a value. It can be assigned a value only once.
    Final method: A final method defined in a base class can’t be overridden by a derived class.
  3. static modifier: The non-access modifier static can be applied to the declarations of variables, methods, classes, and interfaces.
    static variable: static variables belong to a class. They’re common to all instances of a class and aren’t unique to any instance of a class.
    static attributes exist independently of any instances of a class and may be accessed even when no instances of the class have been created.
    A static variable is shared by all the objects of a class. static members belong to a class and not to individual objects.
    The static and final non-access modifiers can be used together to define constants (variables whose value can’t change).
    static method: static methods aren’t associated with objects and can’t use any of the instance variables of a class. You can define static methods to access or manipulate static variables.
    You can’t override the static members in a derived class, but you can redefine them.
    Neither static methods nor static variables can access the non-static variables and methods of a class. But the reverse is true.
    non-static variables and methods can access static variables and methods because the static members of a class exist even if no instances of the class exist.
    static class and interface: You can’t prefix the definition of a top-level class or an interface with the keyword static. But you can define a class and an interface as a static member of another class.
  4. synchronized: A synchronized method can’t be accessed by multiple threads concurrently.
    The synchronized modifier can’t be applied to classes, interfaces, or variables with this modifier.
  5. native: A native method calls and makes use of libraries and methods implemented in other programming languages such as C or C++.
    The native modifier can’t be applied to classes, interfaces, or variables with this modifier.
  6. transient: A transient variable isn’t serialized when the corresponding object is serialized.
    The transient modifier can’t be applied to classes, interfaces, or methods.
  7. volatile: A volatile variable’s value can be safely modified by different threads.
    The volatile modifier can’t be applied to Classes, interfaces, and methods.
  8. strictfp: classes, interfaces, and methods defined using this keyword ensure that calculations using floating-point numbers are identical on all platforms.
    staticfp modifier can’t be used with variables.

Conclusion

Understanding and using the modifiers correctly ensures that you control the accessibility and behavior of your classes, methods, and variables effectively in Java.

Oracle Certified Associate (OCA) certification overview:
Oracle Certified Associate: Java SE 8 Programmer

For more Java and Spring-Boot-related blogs, check out my profile:
https://medium.com/@gauravshah97

Reach me out at:
https://www.linkedin.com/in/gauravshah97/

--

--