Skip to content

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

Last updated on January 17, 2018

Public: Public keyword declared methods and variables can be accessed from, within the class and outside the class

Protected: Protected keyword limits access, it makes your variable/function visible in all classes that extend current class AND its parent classes

Private: Private limits visibility only to class only , private variables/functions are accessible with in the class only.

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.

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.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments