Java Access Specifier

Java access specifier specifies the boundary or scope of a variable/data member, function/method, class, constructor. There are 4 types of Java access specifier:

  1. Public: Applicable for method, class, constructor, variable.
  2. Protected: Applicable for variable, methods.
  3. Default: Applicable for class, constructor, variable, method.
  4. Private: Applicable for methods.

Did you know?

Java also has non-access specifiers like Static(applicable for methods, variables), Final(applicable for class, methods, variable), Abstract(applicable for class, methods), Synchronized and Volatile(applicable for threads).

Accessibility of Java access specifier

java access specifier

Public

  • The public is an access specifier of Java. If a class is public then the file name and class name should be same. Filename refers to the name when one proceeds to create a new Java class and enters a name initially(in Eclipse). And in NotePad, after writing your program we save the file and give a name (classname.java), that is called file name.
  • Java does not support any global variable. This means that everything should be declared inside a class. However, to declare a variable as global, the variable should be declared as public.
  • "Public" is the least restricted Java access specifier.
  • Maximum one public class is allowed within a single file. If the class is declared without any access specifier then it is known as Default class in Java.

For Ex: Let us have a filename with TestClass.java and a class name withPracticeClass, we would see there is a compilation error. (Illustration is shown in Eclipse for better understanding)

java access specifier

Protected

  • Protected access specifier cannot be applied to any class.
  • It is accessible outside a package only through inheritance.

Default

  • Default access specifier cannot be accessed outside the package.
  • If no access specifier is specified by default it is treated as Default.
  • If a class is default then the file name and class name may or may not be same.

For Ex: Let us have a filename with TestClass.java and a class name withPracticeClass, we would see there is no compilation error. (Illustration is shown in Eclipse for better understanding)

java access specifier default

Private

  • Private access specifiers can only be accessed inside the same class.
  • Except for a nested class, a class cannot be private or protected.
  • If a constructor of a class is set to private then the class's instantiation (object of the class) is not possible outside the same class. In other words, instantiation of that particular class would be possible only inside the same class.

An Example to Look At

public class Test {
	static private int x=10;	//static variable
	
}

public class Demo {
public static void main(String[] args) {
	System.out.println(Test.x);// compilation ERROR as x has private access in Test class.

}
}




Let us have a close look at the example. Well, there won't be any output as we would get a compilation error. If you remember we discussed recently that private member cannot be accessed outside the class (only accessible in the class where it is declared). So, over here we are trying to access the value of x which is private to Test class. The variable is called using the class name Test.x since the variable is static. However, as 'x' has private access in Test class so it would be a compilation error.