You are on page 1of 6

Access Specifiers in Java

There are 3 access specifiers in Java: public, private, protected. There is a <no-access-
specifier> i.e a method or class defined without any access specifier.

An access specifier tells us which entity cannot be accessed from where in a program.

Access modifiers can be defined at two levels, Class level and member level.

Class level: There are two access specifiers that are valid at the class level. They are
Public and <none>. So, when we define a class, we can do it either of the ways:

Public class a
{

OR

Class a
{

If a class is defined as public, it can be accessed from outside world i.e from different
packages.
If a class is defined w/o any access specifier, it cannot be accessed from different
packages.

Member Level: Member level means for class variables or class methods level.

Class a
{
<access-specifier> int var;

class a
{
<access-specifier> void method()
{

}
}
Examples:

1. Class a
{
private int number1;
public int number2;
}

2. class a
{
private int number1;
public int number2;

private void method1()


{

public void method2()


{

Private Access specifier: We will be seeing several examples to illustrate the usage
of private access specifier.
Private access specifier can be used with variables and methods in a class.

Case 1: In case of variables, we will see what happens if a variable is declared as


“Private”. We use the keyword ‘Private’ in front of the variable to declare it as a
private variable.

Example:

Class Test
{
private int number;

public void Testmethod()


{
System.out.println(“I want to print the private variable within this
class:”+number);
}
}

public class mainClass {


public static void main(String[] args)
{
Test t = new Test();
t.Testmethod();
}

We have a class defined ‘Test’. That class has a private variable defined ‘number’.
When a variable is declared as private, it can be accessed only from within that class.
So, the variable number can only be accessed by the method Testmethod() belonging
to class Test.

In our main method of class mainClass, we are creating an instance of the class Test,
because this is how we access instance methods.

Another example to illustrate the difference between a variable declared as public Vs


private.

Class Test
{
private int number = 4;
public int number1 = 3;

public void Testmethod()


{
System.out.println(“I want to print the private variable within this
class:”+number);
}
}

public class mainClass {


public static void main(String[] args)
{
Test t = new Test();
System.out.println(t.number);
System.out.println(t.number1);
}

We see a compile time error with the above program, since it is a private variable and
cannot be accessed outside the class.
If a variable is declared as public as in case of ‘number1’ in the above example, we
can access it from outside the class, i.e mainClass. This example clearly illustrates the
difference between variables declared as ‘public’ and ‘private’.

Case 2: Private access specifier can also be used with methods in a class. Below example
illustrates how it can declared and where can it be accessed from.
Example:

Class Test
{
private int number;

private void Testmethod()


{
System.out.println(“I want to print the private variable within this
class:”+number);
}
}

public class mainClass {


public static void main(String[] args)
{
Test t = new Test();
t.Testmethod();
}
}

In the above example, the method ‘Testmethod’ is declared as private. When we try to
execute this program, we see an error saying ‘method has private access in class Test’.
So, how can we use the private methods, infact how can we access them? We can do so
using a method declared as ‘public’ or method with no access specifier.

Example:

Class Test
{
private int number;
public int number1;
private void Testmethod()
{
System.out.println(“I want to print the private variable within this
class:”+number);
}

public void AccessPrivateMethod()


{
System.out.println(“We can access private method declared in this
class here:”);
Testmethod();

}
void noSpecifierMethod()
{
System.out.println(“I can access private method from a non
specifier method too:”);
Testmethod();

public class mainClass {


public static void main(String[] args)
{
Test t = new Test();
t.AccessPrivateMethod();
System.out.println(“ I can print the class variable:”+t.number1);
}
}

Also note that a private variable can be accessed by a private method defined in the same
class. So, anything declared as ‘private’ means only can be accessed in that class and not
from anywhere else.

We covered both ‘public’ and ‘private’ access specifiers in above examples. The other
specifier remaining is ‘protected’ and ‘no access specifier’. We cover ‘protected’ access
specifier when we do Inheritance concept in OOP.

No access specifier: We look into member level access specifier i.e at both variables and
methods level in a class. We would want to look into situations where in no access
modifier is specified.

Example:

Class Test
{
int number;
private void Testmethod()
{
System.out.println(“I want to print the variable with no specifier
within this class:”+number);
}

public class mainClass {


public static void main(String[] args)
{
Test t = new Test();
System.out.println(“ I can print the class variable:”+t.number);
}
}
When a variable’s access specifier is not specified, it can be accessed from with the same
class, from within another class in same package, but not from anywhere outside of this
package.

You might also like