Java provides access control mechanism for classes and methods and other members. Using access modifiers, we can restrict access of the class or it’s methods and other members of the class. Following are the list of access levels available in Java.

  • public
  • default (package level)
  • protected
  • private

Following image summarizes the access modifiers and different access levels allowed for each access modifiers.

java access modifiers Public : This access modifiers, when applied to a class, method or instance variables, makes it visible to every part of the application. This is the access modifier with no restrictions.

Example: Here, class A is accessible from outside of it’s package x and also from a non -sub class B.

package x;
public class A {
public void Message(){
    System.out.println("Message from class A.");
}
}

package y;
import x.*;
public class B {
    public static void main(String args[]){
       A a = new A();
       a.message();
    }
}

Output:

Message from class A.

protected : This access modifier restricts the access to only within the current package and sub classes present in different packages.

Example : Here, class A is extended by class B, which is present in different package has access to it’s super class method message(), which is declared as protected as shown below.

package x;
class A {
    protected void message(){
        System.out.println("Message from class A.");
    }
}
package y;
import x.*;
class B extends A {
   public static void main(String args[]) {
      B b = new B();
      b.message();
   }
}

Output:

Message from class A.

default (package): This access modifier restricts the access of members and methods only within current package. It means, access is not provided out side the current package level.

Example : If a class’s method or behavior is specified with default access modifier, restricts the access to current package level. In the example below, class B defined inside package y is trying to access method of class A, which is defined inside package x will get compilation error.

package x;
class A {
    void message(){
        System.out.println("Message from class A!");
    }
}

package y;
import x.*;
class B {
    public static void main(String args[]) {
      A a = new A();
      a.message();
    }
}

Output :

Compilation Error.

private : This is the most restricted access modifier. declaring a method or member of a class to private restricts it’s access only within that particular class.

Top level classes should not be declared as private or protected. Inner classes or nested classes can be declared as private or protected.

Example : Here, trying to access method message() of class A, which is declared as private gives compilation error.

class A { 
   private void message() { 
     System.out.println("Message from class A"); 
   } 
} 
class B { 
   public static void main(String args[]) { 
      A a = new A(); 
      a.message(); 
   } 
}

Output : 

The method message() from the type A is not visible.

In this article, we learned about different types of access modifiers available in java. It is best practice to declare the class variables as private except for constants.

You also may be interested in following topics.