Access Modifiers
Access Modifiers
Access Modifiers:
A class can use only two modifiers (public and default). whereas a member can use all
the four modifiers.
What does it mean code in one class have access to a member of another class?
1) Whether method code in one class can access a member of another class
2) Whether a subclass can inherit a member of its superclass
Public Members
When a method or variable member is declared public, it means all other classes,
regardless of the package they
belong to, can access the member (assuming the class itself is visible).
Private Members
Members marked private can't be accessed by code in any class other than the
class in which the private member was declared.
What about a subclass that tries to inherit a private member of its superclass?
When a member is declared private, a
subclass can't inherit it.
If the protected keyword is used to define a member, any subclass of the class
declaring the member can access it through inheritance. It doesn't matter if the superclass
and subclass are in different packages
Access modifiers cannot be applied to local variables. Class will not compile.
Only modifier that can be applied will be final.
Final Arguments:
Transient Variables
If you mark an instance variable as transient, you're telling the JVM to skip (ignore)
this variable when you attempt to
serialize the object containing it.
Volatile Variables
The volatile modifier tells the JVM that a thread accessing the variable must always
reconcile its own private copy of the variable with the master copy in memory.