The access modifier in Java defines the access rights of class and variables, methods of that class from outside the class and package. The non-access modifier in Java changes the properties of the class and the variables and methods of that class.
For example, abstract is a non-access modifier, if you add it to the definition of a class, that class becomes an abstract class. And then, no class can initialize the object for this class.
In Java we have the following non-access modifier:
- abstract
- static
- final
- synchronized
- native
- strictfp
- transient
- volatile
In this tutorial, I will cover three basic non-access modifiers, including abstract, static, and final. The other non-access modifiers, I can say through the overview as follows:
- synchronized: This non-access modifier applies only to methods. This is to prevent the method being accessed at the same time by different threads. At a time, only one thread calling the method is marked as synchronized.
- native: non-access modifier also applies only to the method. It is used when you use libraries written in other languages such as C, C ++.
- strictfp: This non-access modifier is used with classes, interfaces, and methods. It is used to ensure that floating point calculation have the same effect on all environments. We can not use this non-access modifier with abstract classes, constructors, and variables.
- transient: This non-access modifier is used in Serialization, which helps to ensure that the state of the variable is not retained during Serialization. You can see more tutorial Talking about transient variable in Java that I wrote. Transient can only be used with variables, not with classes, interfaces, and methods.
- volatile: This non-access modifier is used to update the value of the variable to all threads using this variable whenever the value of the variable changes. It can not be used with classes, interfaces, and methods. You can see more tutorial Talking about volatile variable in Java that I wrote.
OK, now let’s look at three non-access modifiers.
abstract modifier
As I mentioned earlier, when a class is defined with an abstract modifier, another class cannot instantiate objects for this class.
The following example defines an abstract class:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava; public abstract class Student { public String name = "Khanh"; public void goToSchool() { } } |
At this point, we can not initialize the object for the Student class.
An interface by default is an abstract. We do not need to add an abstract modifier when defining an interface, the Java compiler will automatically add it to itself.
Adding the abstract modifier is just redundant:
For a method, when defined with the abstract modifier, it will not implement any code in the body of the method. The implementation of code for this method will occur in the child class inheriting the class that contains this method.
When you define a method with the abstract modifier, the class containing the method must also be defined as an abstract class, otherwise, we will have compile errors.
Abstract modifier cannot be used with variables, any attempt to use it with variables will result in a compile error
final modifier
A class marked with the final modifier cannot be extended from any other class.
For example, we have the Student class defined with the final modifier as follows:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava; public final class Student { private String name = "Khanh"; public void goToSchool() { } } |
And another class will not be able to extend the Student class:
Interface cannot be defined with the final modifier. The reason is that, because an interface if we want to use it, there must be another class that implements it. If it is possible to use the final, how else can the class be implemented? 😀
A method defined with the final modifier cannot be overridden in the child class extends from the class that contains this method. Any attempt to just cause the code to crash compile.
We can use the final modifier with a variable so that the value of the variable will not change after you assign the value to that variable for the first time.
For example:
static modifier
The static variable does not depend on the object of the class and can access this static variable even if no object of that class is initialized.
For example, I have a Student class with the variable name defined as a static variable as follows:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava; public class Student { public static String name = "Khanh"; public void goToSchool() { } } |
then, the variable name can be used without initializing any Student object:
Usually, we use the static and the final modifier to define a variable with an unchanged value, for example:
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava; public class Student { public static final String NAME = "Khanh"; public void goToSchool() { } } |
A static method does not belong to the object of the class, and if a variable or method of the class is used in a static method, it must be static.
For classes and interfaces, if we want to define them as static, we must define them within another class.
For example, we cannot define the static class or interface in the main class:
But we can define it within the Student class.