Signup/Sign In

Access Modifiers in Java

Access modifiers are keywords in Java that are used to set accessibility. An access modifier restricts the access of a class, constructor, data member and method in another class.

Java language has four access modifier to control access level for classes and its members.

  • Default: Default has scope only inside the same package
  • Public: Public has scope that is visible everywhere
  • Protected: Protected has scope within the package and all sub classes
  • Private: Private has scope only within the classes

Java also supports many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient etc. We will cover these in our other tutorial.

Access modifier in java


Default Access Modifier

If we don’t specify any access modifier then it is treated as default modifier. It is used to set accessibility within the package. It means we can not access its method or class fro outside the package. It is also known as package accessibility modifier.

Example:

In this example, we created a Demo class inside the package1 and another class Test by which we are accessing show() method of Demo class. We did not mentioned access modifier for the show() method that’s why it is not accessible and reports an error during compile time.

Demo.java

  
package package1;

public class Demo {
  
  int a = 10;
  // default access modifier
  void show() {
    System.out.println(a);
  }

}
  

Test.java

  
import package1.Demo;
public class Test {
   
  public static void main(String[] args) {
    Demo demo = new Demo();
    demo.show(); // compile error
  }
}
   

The method show() from the type Demo is not visible

Public Access Modifier

public access modifier is used to set public accessibility to a variable, method or a class. Any variable or method which is declared as public can be accessible from anywhere in the application.

Example:

Here, we have two class Demo and Test located in two different package. Now we want to access show method of Demo class from Test class. The method has public accessibility so it works fine. See the below example.

Demo.java

  
package package1;

public class Demo {
  
  int a = 10;
  // public access modifier
  public void show() {
    System.out.println(a);
  }

}
  

Test.java

  
package package2;
import package1.Demo;
public class Test {
   
  public static void main(String[] args) {
    Demo demo = new Demo();
    demo.show();
  }
}
  

10

Protected Access Modifier

Protected modifier protects the variable, method from accessible from outside the class. It is accessible within class, and in the child class (inheritance) whether child is located in the same package or some other package.

Example:

In this example, Test class is extended by Demo and called a protected method show() which is accessible now due to inheritance.

Demo.java

  
package package1;

public class Demo {
  
  int a = 10;
  // public access modifier
  protected void show() {
    System.out.println(a);
  }

}
  

Test.java

  
package package2;
import package1.Demo;
public class Test extends Demo{
   
  public static void main(String[] args) {
    Test test = new Test();
    test.show();
  }
}
  

10

Private Access Modifier

Private modifier is most restricted modifier which allows accessibility within same class only. We can set this modifier to any variable, method or even constructor as well.

Example:

In this example, we set private modifier to show() method and try to access that method from outside the class. Java does not allow to access it from outside the class.

Demo.java

  
class Demo {
  
  int a = 10;
  private void show() {
    System.out.println(a);
  }

}
  

Test.java

  
public class Test {
   
  public static void main(String[] args) {
    Demo demo = new Demo();
    demo.show(); // compile error
  }
}
  

The method show() from the type Demo is not visible

Non-access Modifier

Along with access modifiers, Java provides non-access modifiers as well. These modifier are used to set special properties to the variable or method.

Non-access modifiers do not change the accessibility of variable or method, but they provide special properties to them. Java provides following non-access modifiers.

  1. Final
  2. Static
  3. Transient
  4. Synchronized
  5. Volatile

Final Modifier

Final modifier can be used with variable, method and class. if variable is declared final then we cannot change its value. If method is declared final then it can not be overridden and if a class is declared final then we can not inherit it.

Static modifier

static modifier is used to make field static. We can use it to declare static variable, method, class etc. static can be use to declare class level variable. If a method is declared static then we don’t need to have object to access that. We can use static to create nested class.

Transient modifier

When an instance variable is declared as transient, then its value doesn't persist when an object is serialized

Synchronized modifier

When a method is synchronized it can be accessed by only one thread at a time. We will discuss it in detail in Thread.

Volatile modifier

Volatile modifier tells to the compiler that the volatile variable can be changed unexpectedly by other parts of a program. Volatile variables are used in case of multi-threading program. volatile keyword cannot be used with a method or a class. It can be only used with a variable.