Posts

Showing posts with the label Java-access-specifiers

Java Access specifiers explained

Java provides a mechanism to precisely control access to the various members of a class via access specifiers. In this blog post, I will be explaining Java’s access specifiers i.e. private, protected, public and default. Private When a member of a class is specified as private then that member can only be accessed only by other members of its class. However, it will not accessible outside of the class via the dot operator. All private fields need to have getter and setter methods to retrieve and set their values. Otherwise, they will not be accessible outside of the class.. Public When a member of a class is modified by the public specifier, then that member can be accessed by any other code. It is not generally recommend to have this access specifiers for fields of a class. All fields of a class should have private access specifiers and public getter and setter methods to access them. Default access Specifier The default access specifier comes into play when no other access spe...