Access Modifiers and Specifiers in Java

Access Modifiers :
An access modifier is a Java keyword that indicates how a field or method can be accessed. Variables and methods in Java have access restrictions, described by the following access modifiers:

private: 
  • Used for most instance variables
  • private variables and methods are accessible only to methods of the class in which they are declared
  • Declaring instance variables private is known as data hiding
  • Example:  private int x;
default (this means no modifier is used):
  • Access is limited to the package in which the member is declared
  • Example:  int x; 
protected:
  •  Access is limited to the package in which the member is declared, as well as all subclasses of its class
  •  Example:  protected void setName() { . . . }
public:
  • The member is accessible to all classes in all packages.
  •  Declaring public methods is know as defining the class’ public interface.
  • Example:  public String getName() { . . . }
Java Access Specifiers :
  • Access specifiers indicates which mebmers of a class can be used by other classes
  •  We use of public, protected and private for access specifications
  •  Packeg access is used when there is no access specifier
  •  Package access means that all classes in the same package can access the member
  •  But for all other classes the member is private
You might also likefollowing tutorials:





Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email