Home           J2EE Concepts           JMS           Java Language           Contact           Back           Next           Bookmark this Page          










Some useful tips related to:
 - health & fitness
 - vehicle care
 - cooking
 - misc household tasks

Access Control


Access control is a mechanism by which we can control what data should be accessible & to whom it should be accessible. You can specify whether a data member or method of a class is accessible to the rest of the world, or it is accessible to some specific classes or it is accessible only to the class that defined it.

The access of any data/method is controlled by access modifiers. Java provides following access specifiers:
- public
- private
- protected
- If no access level is defined, then Java applies default access control.


If a data member/method is preceded by public specifier, then it can be accessed by any code. That is why main method in a class is always declared public (because it is called by JRE; i.e. a code that is outside the class).

If a data member/method is preceded by private specifier, then it can only be accessed by other members of the class that has defined it.

If a data member is preceded by protected specifier, then it can be only be accessed by members of its class and members of its children classes; i.e. protected comes into picture only when inheritance is involved.

In case no access specifier is provied, then the data member is public within its own package & to it's subclasses; i.e. any class within same package can instantiate object of this class & access the data member.


A class can have only two visibilities: public & default. A public class can be accessed by any code (i.e. any code can create instance of this class, access it's static members); whereas a class with default access is only accessible to code in same package.