In this tutorial, we will look at different Access specifier in Java.

Java provides 4 different Access specifier..

1. Private.
2. Protected.
3. Default.
4. Public.

We will see how these access specifier are specified and how they are useful.

Any access specifier you define for a variable should be specified while declaring or initializing the variable,in the form

(in case of default access specifier, it’s just.

In this Part, we will check how the specifier behave when the classes defined in
same package are trying to access member variables of other classes.

Consider the following code,

package com.vk;

public class Vk {

private int a=40; // Access Specifier -> Private.

}

class Vk1
{
public static void main(String args[])
{
System.out.println(“Creating a new Object”);
Vk v=new Vk();

System.out.println(“Trying to access the Private member variable”);
v.a=50;

}
}

In the above program, we have a defined a member variable ‘a’ with Access
Specifier -> Private and we are trying to access this variable from a class ‘Vk1’.
Please note that both the classes are defined in the same package -> com.vk

Run this program and check if the compilation is success.

Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The field Vk.a is not visible

at com.vk.Vk1.main(Vk.java:17)

Private Variables cannot be accessed by any other class, they can only be accessed
in the class where they are defined.

Protected Access Specifier

Now lets change the access specifier for our variable ‘a’ to protected and check if
compilation is clean.

package com.vk;

public class Vk {

protected int a=40; // Access Specifier -> Protected.

}

class Vk1
{
public static void main(String args[])
{
System.out.println(“Creating a new Object”);
Vk v=new Vk();

System.out.println(“Trying to access the Protected member variable”);
v.a=50;

}
}

OUTPUT

Creating a new Object
Trying to access the Protected member variable

Protected variables can be accessed by the derived class.

Default Access Specifier

To define a variable with default access specifier, don’t mention anything before
the data_type.

Now lets change the access specifier for our variable ‘a’ to default and check if
compilation is clean.

package com.vk;

public class Vk {

int a=40; // Access Specifier -> Default.

}

class Vk1
{
public static void main(String args[])
{
System.out.println(“Creating a new Object”);
Vk v=new Vk();

System.out.println(“Trying to access the Default member variable”);
v.a=50;

}
}

OUTPUT

Creating a new Object
Trying to access the Default member variable

Default variables can be accessed by any other class defined in the same package.

Public Access Specifier

Now lets change the access specifier for our variable ‘a’ to Public and check if
compilation is clean.

package com.vk;

public class Vk {

public int a=40; // Access Specifier -> Public.

}

class Vk1
{
public static void main(String args[])
{
System.out.println(“Creating a new Object”);
Vk v=new Vk();

System.out.println(“Trying to access the Public member variable”);
v.a=50;

}
}

OUTPUT

Creating a new Object
Trying to access the Public member variable