Java

Modifiers in Java

A modifier in Java is used to define the accessibility and behaviors of the classes, their constructors, fields, and methods. There are two types of modifiers in Java, access and non-access. The access modifiers define the accessibility whereas the non-access modifiers report the behavioral information of classes, constructors, and methods to the JVM. This article provides a detailed guide on modifiers in Java with the following learning outcomes:

  • types of modifiers
  • how modifiers work in Java

Types of modifiers

This section considers the modifier types in detail, additionally, the sub-types of each type are also described.

Access Modifiers

These modifiers are responsible for defining the accessibility of the classes, constructors, method, etc. For this, the access modifiers are further divided into several sub-types.

Default: This default access modifier is assigned when no other modifier is used. The members of a default class are accessible within the package where the class resides.

Public: This is the most used and common access modifier. The methods or data members are declared using public keywords and can be accessed from anywhere without any restriction.

Note: The main class can be created using the default or public keywords. However, the methods/constructors/attributes can be created as private, protected, default, and public

Private: The data members of the private class are declared using private keywords and are accessible only within the parent class. Moreover, any other method from any other class cannot access the members of the private class. As main class cannot be declared using a private keyword, so, the nested classes or methods practice the private keyword.

Protected: Members of a package with a protected access modifier are accessible within the same package of the same subclass.

Non-Access Modifiers

The JVM uses these modifiers to find out how classes behave. The following types of non-access modifiers can be used:

Final: The final non-access modifiers are used with the classes to restrict their inheritance. The final keyword is used with the classes to define their un-inherited behavior and JVM then does not allow any subclass to extend such kind of class. Similarly, the methods and variables can also be used with the final non-access modifiers to restrict their behavior.

Abstract: The abstract classes are only inherited not instantiated. The primary purpose of the abstract classes is to extend them to make use of these classes. Moreover, the abstract methods are also like abstract classes, they are declared in a superclass, but they are instantiated in the subclass at the time of extending the sub-class.

Note: The final and abstract keywords can be used to define the main class. The methods and attributes can be defined using the non-access modifiers like static, transient, synchronized.

  • Synchronized: The synchronization concept comes into action where multiple programs are executed to minimize resource consumption. The synchronized non-access modifier helps in restricting one method to access by multiple threads. This non-access modifier is only applicable to methods.
  • Static: This non-access modifier is applicable to the inner classes, variables, methods. In the static classes, variables are associated with the class, and any change to variables is distributed to all the objects. Moreover, the static methods can access static variables or other static methods of the same class.
  • Native: The native keyword is used to identify that the method is implemented in native code using Java Native Interface. Usually, the methods implemented in C/C++ are considered as the native methods.
  • Transient: The transient non-access modifier is used with the class names and is practiced to secure receiving of data over the network. The transient keyword is used with the class names and if the class is transient then the data members need not to transient. You would have to use transient keywords with the data member that does not need serialization.
  • Strictfp: It stands for strict floating-point and restricts the floating-point calculations to ensure the result is the same across various platforms. The floating-point calculations are platform-dependent and return different results on various platforms.

Conclusion

The modifiers in Java can be of either access or non-access type. Data members and methods of a class are made accessible by access modifiers. Whereas the non-access modifiers notify JVM about the behavior of data members/methods of a class. This informative post describes the access and non-accessmodifiers along with their types/subtypes in Java. After going through this post, you would have learned to choose the appropriate modifiers for your class members in Java.

About the author

Adnan Shabbir