|
You can apply both, the access modifiers and other non-access modifiers, to the member variable declaration. We have already seen the effects of access modifiers on member variables. When you apply non-access modifiers to member variable, they tell you more about the way object’s state is stored in that variable. The four modifiers that you can apply to a member variable are- static, final, transient and volatile. Let us see the general format for member variable declaration with modifiers:
[Access Modifier] [static] [final] [transient] [volatile] type variableName
The modifiers can appear in any order in the declaration which means you can place the access modifier (if any) before/after/in-between the non-access modifiers. The items between [] are optional. Here is a quick review of what each of the modifiers does to the value in the variable:
§ An access modifier decides classes that can access the value in this variable.
§
A static
modifier indicates that the variable is a class variable as opposed to
an instance variable which means that this variable is associated with
class rather than an individual instance and all the instances of that class
will share the value of this variable.
§
A final
modifier indicates that the value of the variable is unchangeable.
§
A transient
modifier marks the variable so that its value cannot be stored as a part of the
object's persistent state.
§
A volatile
modifier makes sure that all the threads access the same copy of the variable
We have already seen how access modifiers are applied to variables. Let us discuss each of the non-access modifiers for the variables.