In this tutorial, we will understand all non-access modifiers in Java. We know that access modifiers such as private, default, protected, and public restrict the visibility (accessibility) of classes, fields, methods, or constructors in Java.
These access modifiers basically define the boundary for accessing members of a class and a class itself. Except for these modifiers, all the remaining modifiers available in Java are called non-access modifiers. They are:
- abstract
- final
- native
- static
- strictfp
- synchronized
- transient
- volatile
Let’s understand one by one in brief about Java non-access modifiers and their usage.
Abstract Modifier in Java
1. Abstract is a keyword that can be applied with outer class, inner class, method, outer interface, and inner interface. It cannot be applied with variable, constructor, block, and enum.
2. When a class is declared with a keyword abstract, it is called abstract class in java.
3. An abstract keyword cannot be simultaneously declared with the final keyword.
4. An interface in Java is by default abstract and does not need to be declared abstract.
5. When a method is declared with an abstract keyword, it is known as abstract method. It contains only a signature and no body. If you declare a method as abstract in a class, the class must be declared as an abstract class.
6. Abstract keyword cannot be declared simultaneously with final, private, native, static, or synchronized.
For best practice example programs based on abstract keyword, go to this tutorial: Abstraction in Java
Final Modifier in Java
1. A final is a keyword that can be applied with outer class, inner class, variable, and method. It cannot be applied with interface, constructor, block, and enum.
2. A final class cannot be extended.
3. A final method cannot be overridden in java.
4. Once a final variable is declared and initialized, it cannot be changed.
For practice, example programs step by step, go to this tutorial: Final Keyword in Java
Native Modifier in Java
1. A native modifier can be used only with a method. It contains only a signature but not a body.
2. A native method is generally used to merge other programming languages like C and C++ code into Java programming.
3. A native keyword cannot be used with strictfp simultaneously.
Static Modifier in Java
1. A static modifier can be applied with inner class, variable, method, block, and inner interface. It cannot be declared with top-level class, outer interface.
2. A method declared with static keyword can be accessed through the class name. It is generally used to access the static variable.
3. A static variable or static data member is also accessed through class name.
Strictfp Modifier in Java
1. A strictfp modifier can be applied with outer class, inner class, method, and outer interface. It cannot be applied with variable, block, inner interface, constructor, and enum.
2. A strictfp class uses the IEEE 754-1985 floating-point specification for all of its floating-point operations.
3. The method defined within the interface cannot be declared with strictfp modifier.
4. Stricfp modifiers cannot be used with native modifiers simultaneously.
Synchronized Modifier in Java
1. A synchronized modifier in Java can be applied only with method and block.
2. In the synchronized method block, only one thread is allowed to execute at a time. It makes thread-safe. Therefore, the synchronized method or synchronized block is mainly used for thread safety.
3. Statements can also be synchronized in Java.
Transient Modifier in Java
1. The transient modifier can be applied only with variables or data members.
2. When a class is serialized then transient data member is not serialized.
Volatile Modifier in Java
1. A volatile modifier can be applied only with variables. It is a keyword.
2. If a data member or variable is declared as volatile, all threads can see consistent the value of variable in java memory.
3. A volatile keyword cannot be applied with a method declaration.
4. Volatile does not create any kind of lock on the variable in the program.
Hope that this tutorial has covered almost all important points related to non-access modifiers in Java. I hope that you will have understood the basic points of non-access modifiers and enjoyed it.
In the next tutorial, we will understand instance initialization block in Java with the help of important example programs.
Thanks for reading!!!