Introduction
In this article, I am explaining Access Modifiers keywords in Java.
In access modifiers there are mainly the following three keywords:
- Private
- Protected
- Public
Here's the explanation:
Java Access Modifier - Private Keyword
The private keyword is an access modifier that can be applied to a method, member variable and inner class.
- Private Method
If a method marked as private, it cannot be invoked from outside the class it is declared. In other words, the private method is available to the enclosing class. For example:
- class A
- {
- private void foo()
- {
- }
- void bar()
- {
- foo(); // okay, no problem!
- }
- }
- class B
- {
- void woo()
- {
- A a = new A();
- a.foo(); // Oh no! since foo() is private
- }
- }
- Private Variable
The private access modifier can also be applied to member variables which are declared within a class. Like private method, a private variable can be accessed only within its enclosing class. For example:
- class A
- {
- private int number;
- void bar()
- {
- number = 10; // OK
- }
- }
- class B
- {
- void foo()
- {
- A a = new A();
- a.number = 10. // Oh no! since number is private
- }
- }
- Private inner class
An inner class is one that is declared inside another class. An inner class can be declared as private thus it can be accessed only from within the enclosing class, like private method and private variable. For example:
- class A {
- private class SubA {
- // inner class
- }
- void bar() {
- SubA obj = new SubA(); // OK
- }
- }
- class B {
- void foo() {
- A.SubA a = new A.SubA(); // Oh no! since SubA is private
- }
- }
Java Access Modifier - Protected Keyword
The protected keyword is an access modifier for method and variable of a class. When a method or a variable is marked protected, it can be accessed from the following:
- Within the enclosing class.
- Other classes in the same package as the enclosing class.
- Sub classes, regardless of packages.
The main purpose of protected keyword is to have the method or variable that can be inherited from sub classes.
Example
The following class Person declares a protected variable name, inside package p1:
- package p1;
- public class Person {
- protected String name;
- }
- package p1;
- public class Employer {
- void hireEmployee() {
- Person p = new Person();
- p.name = "Nam"; // access protected variable directly
- }
- }
- package p2;
- import p1.Person;
- class Employee extends Person {
- void doStuff() {
- name = "Bob";
- }
- }
- package p2;
- import p1.Person;
- class AnotherEmployer {
- void hire() {
- Person p = new Person();
- // compile error, cannot acceess protected variable
- // from different package
- p.name = "Nam";
- }
- }
Java Access Modifier - Public keyword
The public keyword is an access modifier for class, method and variable:
- When a class is marked public, it can be accessed from anywhere, including outside packages.
- When a method is marked public, it can be invoked not only from the enclosing class, but also from outside classes.
- When a variable is marked public, it can be accessed and updated from outside classes.
To summarize, public is the access modifier that has least restriction on the object it modifies.
The following code example shows a public class which has a public method eat() and a public variable name:
- public class Person {
- public String name;
- public void eat() {}
- }

SubashPosted Aug 1, 2016, 5:13 AM
Nice
Santhakumar MunuswamyPosted Dec 28, 2015, 1:30 PM
Thanks for sharing
Gopi ChandPosted Nov 18, 2015, 2:06 PM
Nice
Harshad PansuriyaPosted Nov 3, 2015, 5:19 AM
Thanks Sabyasachi Mishra
Sabyasachi MishraPosted Nov 3, 2015, 5:16 AM
Good one
Harshad PansuriyaPosted Nov 2, 2015, 5:06 AM
Thanks Anu Vivin
Anu VPosted Nov 2, 2015, 4:54 AM
This is really Good Article..
Harshad PansuriyaPosted Oct 31, 2015, 7:14 AM
Thanks Sibeesh Venu
Sibeesh VenuPosted Oct 31, 2015, 6:14 AM
Nice Share
Harshad PansuriyaPosted Oct 30, 2015, 11:59 PM
Thanks Priyaranjan K S
Harshad PansuriyaPosted Oct 30, 2015, 11:58 PM
Thanks Gowtham K
Harshad PansuriyaPosted Oct 30, 2015, 11:58 PM
Thanks Nilesh Jadav
Priyaranjan K SPosted Oct 30, 2015, 1:37 PM
Thanks for the share
Gowtham KPosted Oct 30, 2015, 12:48 PM
Good One
Nilesh JadavPosted Oct 30, 2015, 11:47 AM
Good Work