Access modifiers, as the name itself suggests, are used to control the accessibility/visibility of variables, methods, constructors of a class inside another class. Java provides some keywords to accomplish the same, these keywords are known as access modifiers. These modifiers decides whether other classes can use a particular variable, method, constructor of a class or not. There are four types of access modifiers in java.
- Private (declared using
privatekeyword) - Default or package private (when no keyword is used)
- Protected (declared using
protectedkeyword) - Public (declared using
publickeyword)
All these modifiers can be used with variables, constructors and methods of a class while Default and Public access modifiers
can also be used with top level classes and interfaces. To understand this tutorial easily, you must have knowledge about
packages in java, if not refer the packages tutorial first.
An access modifier must be used before the data type of a variable and return type of a method. In case of class,
interface or enum it must be used before the class,
interface and enum keyword. Some of the examples of using access modifiers with different members are :
privateintcount = 0;// CorrectpublicString print() { }// Correctpublic classMyClass { }// Correctint protectedid = 20;// Wrongvoid publicgetDetail() { }// Wrong
Private Access Modifier in Java
A member is declared private using private keyword. This is the most restrictive access modifier in java. The code below declares the variable
count as private.
privateintcount = 0;
. Some key points about private access modifier are :
- A
privatemember is accessible within it's own class only. For example a variablecountdefined asprivate, inside classAwill be accessible within classAonly, no other classes can access this variable. - Variables, methods and constructors can be declared as
private. - Top level classes and interfaces can not be declared as
private, where as nested classes and interfaces can be declared asprivate. - If a class has
privateconstructor then we cannot create the object of that class in another class.
Private access modifier program in Java
classPrivateModifier {private intcount = 10;private voidprintMessage() { System.out.println("count = : "+count); } }classPrivateModifierTest {private intnum = 20;public static voidmain(String [] args) { PrivateModifierTest pmt =newPrivateModifierTest(); pmt.printDetail(); System.out.println("num = "+pmt.num); PrivateModifier pm =newPrivateModifier();// pm.printMessage();// compilation error// System.out.println(pm.count);// compilation error}private voidprintDetail() { System.out.println("Private modifier program"); } }
Output:
Private modifier program
num = 20
As you can see in above program class PrivateModifierTest is able to access it's private variable and method but if
you uncomment last two line in PrivateModifierTest class it will result
in compilation error as it can not access private members of PrivateModifier class.
Default Access Modifier in Java
When a member is declared without any access modifier, it is called default modifier. So default is not a keyword, it's
just when we don't use any access modifier, it is called default or no modifier. It is also known as package-private access modifier.
The code below declares the variable count as default.
int count = 0;
Some key points about default access modifier are :
- A default member is accessible within the package only, that is why it is also known as package-private modifier. For example a variable
countdefined with no modifier inside classAcan be accessed by a classBif classBis in same package as classA. - Any member can be declared with no modifier whether it's a variable, method, constructor, class, interface or anything else.
- Nested classes and interfaces can also be declared with no modifier.
Default access modifier program in Java
packagemypack;classDefaultModifier {intcount = 10;voidprintMessage() { System.out.println("Default modifier program"); } }public classDefaultModifierTest {public static voidmain(String [] args) { DefaultModifier dmf =newDefaultModifier(); dmf.printMessage(); System.out.println("count = "+dmf.count); } }
Output:
Default modifier program
count = 10
As you can see in above program class DefaultModifierTest is able to access the default variable and method of class DefaultModifier, since both classes
are in same package mypack. If both classes were in different packages, this program would have resulted in compilation error.
Protected Access Modifier in Java
A member is declared protected using protected keyword. Code below declares the variable count as protected.
protected int count = 0;
Some key points about protected modifier are :
- A
protectedmember can be accessed within it's own package as well as by a subclass in another package. For example a variablecountdefined withprotectedmodifier inside classAcan be accessed by classBif classBis in same package as classAor classBis in different package but it's a subclass of classA. - This access modifier can be used with variable, method and constructors.
- Top level classes and interfaces can not be declared as
protected, where as nested classes and interfaces can be declared asprotected.
Protected access modifier program in Java
packagetestpack;public classProtectedModifier {protected intcount = 10;protected voidprintMessage() { System.out.println("Protected modifier program"); } }
packagemypack;importtestpack.ProtectedModifier;public classProtectedModifierTestextendsProtectedModifier {public static voidmain(String [] args) { ProtectedModifierTest pmf =newProtectedModifierTest(); pmf.printMessage(); System.out.println("count = "+pmf.count); } }
Output:
Protected modifier program
count = 10
Here both classes are in different packages but ProtectedModifierTest class is a subclass of ProtectedModifier class that is why
it is able to access the variable and method of it's super class.
Note : It must be the object of subclass to access the protected member of super class if both the classes are in
different packages. For example in above program we can not access the protected member of ProtectedModifier class by creating it's object
in ProtectedModifierTest class.
Public Access Modifier in Java
A member is declared public using public keyword. This modifier doesn't put any restriction, it can be accessed anywhere.
The code below declares the variable count as public.
public int count = 0;
Some key points about public modifier are :
- A
publicmember is accessible everywhere, no matter whether it's in same package or in different package. - Any member can be declared with
publicmodifier whether it's a variable, method, constructor, class, interface or anything else.
Public access modifier program in Java
packagetestpack;public classPublicModifier {public intcount = 10;public voidprintMessage() { System.out.println("Public modifier program"); } }
packagemypack;importtestpack.PublicModifier;public classPublicModifierTest {public static voidmain(String [] args) { PublicModifier pmf =newPublicModifier(); pmf.printMessage(); System.out.println("count = "+pmf.count); } }
Output:
Public modifier program
count = 10
As mentioned above public modifier doesn't apply any restriction that is why public members of PublicModifier
class is accessible in PublicModifierTest class even though both classes are in different packages.
Java access modifiers table
The following tables shows the accessibility/visibility scope of a member for different access modifiers in java.
| Operator | Within Class | Within package | Subclass in different package | Everywhere |
|---|---|---|---|---|
| private | Yes | No | No | No |
| default | Yes | Yes | No | No |
| protected | Yes | Yes | Yes | No |
| public | Yes | Yes | Yes | Yes |
Can I declare the variables of a method with public, private or protected modifiers ?.
No, variables inside a method can not be declared with public, private or protected modifier.
They must be declared without any modifier.
Note : A member declared inside another member will be accessible outside only if it's outer member has same or greater accessibility modifier. For example if you
declare a public variable inside a class which has default modifier, then the variable will not be accessible outside the package as it's
class will be accessible within the package only.
- A program file can have only one public class, as each public type in java must be saved in it's own file name.
- A program can not contain public class and public interface together.
- There can be multiple classes with default modifier in a program.
- If you want your member's of a class should be accessible to everyone then declare your class and member both as public.
- You should use access modifiers for a member wisely, giving unnecessary access to others limits the flexibility of changing your code.


