E
x
c
e
l
P
T
P

What are the differences between public, private, protected,static, transient, final and volatile?

March 4, 2014

Public: Public declared items can be accessed everywhere.

Protected: Protected limits access to inherited and parent classes (and to the class that defines the item).

Private: Private limits visibility only to the class that defines the item.

Static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

Final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Transient: A transient variable is a variable that may not be serialized.

differences between public, private, protected,static
Volatile: a variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.