These methods have different scopes in Java. Like, the final modifier is applicable to classes, methods, and variables whereas native and synchronized modifier are applicable on methods only. Keeping in view the importance of non-access modifiers, this descriptive guide enlists and explains the non-access modifiers in Java.
Non-access modifiers in Java
Let’s elaborate the non-access modifiers one by one:
Final non-access modifiers
The final non-access modifier is used to limit the number of declarations for a class, method, and variable. The applicability of the final modifier is described as:
- a class declared with the final keyword cannot be extended further
- a variable’s value cannot be altered if it is declared with the final keyword
- a final method cannot be overridden by other classes.
Example
final class NonAccessMod{
public void myfunc(){
System.out.println("Super Class!");
}
}
class LinuxHint extends NonAccessMod{
public void myFun1(){
System.out.println("Second Class");
}
}
The above code tries to extend the class NonAccessMod that is declared using the final keyword.
From the output, it is observed that the final class named NonAccessMod cannot be extended as it was declared using the final keyword.
Synchronized non-access modifiers
The synchronized non-access modifier is only applicable to methods and synchronized methods and it can only be accessed by one thread at a time which results in maintaining the flow of the program.
Example
The code written above shows the declaration of the synchronized method.
Abstract Non-Access Modifiers
The abstract non-access modifiers are applicable to methods and classes.
- A class declared using the abstract keyword is recognized as an abstract class or the class that has abstract methods is also known as the abstract class.
- Abstract methods do not contain a body, they have signatures only.
Example
public abstract class deft {
}
The above code creates an abstract class using the abstract keyword.
Static non-access modifiers
This non-access modifier is applicable to methods, variables, and nested classes.
- A static variable has only one copy which is distributed across the objects. A single change to that variable will change its value in all objects.
- the static methods contain static data members or other static methods
Example
The code provided below initializes the static variables and is used in the static main method.
class statmod {
static int x=4, y=6;
public static void main(String args[])
{
System.out.println("x+y=" + (x+y));
}
}
Output
Strictfp Non-access Modifier
The strictfp (strict floating point) forces methods/classes to stick to IEEE-754 standards to ensure the accuracy of the output irrespective of the hardware dependencies.
Example
strictfp class strfp{
strictfp void main(String args[])
{
System.out.println("Welcome to LinuxHint!");
}
}
The above code creates a strictfp class and strictfp method.
Output
Transient non-access modifier
The transient modifier is used to avoid the serialization of any data member. The transient modifier is quite helpful to declare sensitive data members security-related queries. For instance, if you use any credentials and do not want to store the original credentials then you can use the transient keyword. By using the transient keyword, the default value of the datatype is stored.
Native non-access modifiers
Native modifiers are used to indicate that the method(as it is only applicable to methods) is implemented in native code. The methods implemented in C/C++ are referred to as native methods. The purpose of native modifier is to show that the method is being implemented in platform dependent code(C/C++).
Conclusion
The non-access modifiers tell the behavior of the classes, methods, and variables to the JVM. For this, seven modifiers are regarded as non-access modifiers. Throughout this article, we will explore the non-access modifiers that Java supports. Each modifier has its own applicability scope, such as various non-access modifiers can be applied only to methods and few are applicable to methods, classes, and variables.