Loading
Modifiers in Java define the scope, access level, and behavior of classes, methods, and variables. They help control how code interacts within and outside the application.

 Types of Modifiers

Java modifiers are categorized into two types:

1.  Access Modifiers
2.  Non-Access Modifiers 



Access Modifier:
Access modifiers are those keywords that describe the rule of the access level of Classes, interfaces and their members (i.e. Constructors, methods, variables, etc.)

Example:

package quipohouse; class HelloWorld { public void method1() { // It can be accessed by other classes } private void method2() { // It can’t be accessed by other classes } protected void method3() { // It can be accessed within the package and by subclasses } }

Their are Four types of Access modifiers:
1. Default Access Modifier (No Modifier, Package-Private):
  • If no modifier is specified, it is accessible only within the same package.
  • Cannot be accessed outside the package.
Example:
class Demo { void show() { System.out.println("Accessible within the same package!"); } }


2. Public Access Modifier:

  • Can be accessed from anywhere in the program.
  • Used for methods, classes and variables that should be globally accessible.
Example:

//File 1 : HelloWorld.java

package quipohouse;
public class HelloWorld {

  public void message() {

    System.out.println(“Welcome to Quipo House”);
  }
}
//File 2 : Main.java

package quipohouse;
public class Main
{
  public static void main(String[] args)
  {
    HelloWorld obj = new HelloWorld ();
      obj.message ();
  }
}

3. Private Access Modifier:

  • Class member marker as private cannot access outside its class.
  • Except for the nested class, A class cannot be private.

Example:

//File : Main.java

package quipohouse;
class HelloWorld
{
private void message()
    {
        System.out.println("Quipo House");
    }
}
public class Main
{
public static void main(String args[])
    {
        HelloWorld obj = new HelloWorld ();
        //Trying to access private method message from another class
        obj.message();
    }
}

Example of the Access level of private modifier with class members (method, variable, etc.):

//File : Main.java

package quipohouse;
class Message
{
  private String msg;
  public String getMessage ()          // getter method
  {
    return this.msg;
  }
  public void setMessage (String msg)   // setter method
  {
    this.msg = msg;
  }
}

public class Main
{
  public static void main (String[]main)
  {
    Message m = new Message ();

      // accessing the private variable using the getter and setter method
      m.setMessage ("Welcome To Quipo House");
      System.out.println (m.getMessage ());
  }
}

Output:

Welcome To Quipo House

4. Protected Access Modifier:

  • When the methods and variables are declared with a protected access modifier, we can access them within the same package as well as from its subclasses even outside the package where it it declared.
  • A class cannot be protected.
Example:

//File : HelloWorld.java

public class HelloWorld
{

  protected void message ()
  {
    System.out.println ("Welcome to Quipo House ");
  }
}
//File : Main.java

public class Main extends HelloWorld
{
  public static void main (String[]args)
  {
    HelloWorld obj = new HelloWorld ();
      obj.message ();
  }
}


NOTE: Top-level Classes cannot be private and protected.

Access ModifiersClassPackageoutside the package
PrivateYesNoNo
DefaultYesYesNo
ProtectedYesYesOnly in the Derived class
PublicYesYesYes

Non-Access Modifier :

The Non-access modifier affects the code behaviour other than its visibility or scope.

There are six non-access modifiers. We will separately study it further in this tutorial.

  • static: Belongs to the class rather than instances (shared among all instances).
  • final: Prevents inheritance (for classes), method overriding, or variable reassignment.
  • abstract: Defines abstract methods (for classes) that must be implemented by subclasses.
  • synchronized: Ensures that a method or block of code is accessed by only one thread at a time.
  • transient: Fields marked with  transient are skipped during the serialization process, meaning their values will not be saved or transmitted when an object is serialized.
  • synchronized: Ensures that a method or block of code is accessed by only one thread at a time.