Skip to content

Modifiers

Modifiers in Java

Modifiers in Java are keywords that define the scope, accessibility, and behavior of classes, methods, constructors, and variables. They are broadly classified into two categories:

  1. Access Modifiers
  2. Non-Access Modifiers

1. Access Modifiers

Access modifiers control the visibility of classes, methods, constructors, and variables.

Types of Access Modifiers:

ModifierClassPackageSubclassWorld
publicYesYesYesYes
protectedYesYesYesNo
defaultYesYesNoNo
privateYesNoNoNo
  • public: Allows access from anywhere.
  • protected: Allows access within the same package and to subclasses outside the package.
  • default (no modifier): Allows access only within the same package.
  • private: Allows access only within the same class.

Examples:

public class Test {

    private int data = 10;   // Accessible only within this class

    protected int value = 20; // Accessible within the same package and subclasses

    public void display() {  // Accessible from anywhere

        System.out.println(“Hello, World!”);

    }

}


2. Non-Access Modifiers

Non-access modifiers provide functional properties to classes, methods, and variables.

Types of Non-Access Modifiers:

ModifierUse
staticIndicates a class-level variable or method (shared by all objects).
finalPrevents modification (variable value cannot change, methods cannot be overridden, classes cannot be subclassed).
abstractDefines a method without implementation or a class with incomplete implementation.
synchronizedEnsures thread safety in concurrent execution.
volatileIndicates a variable can be modified by multiple threads.
transientPrevents serialization of a field.
nativeDeclares a method implemented in native code (e.g., C/C++).
strictfpEnforces strict floating-point calculations.

Key Non-Access Modifiers:

  1. static
    1. Used for variables and methods that belong to the class, not to specific objects.
    1. Example:

class Counter {

    static int count = 0; // Shared across all objects

    Counter() {

        count++;

    }

}

public class Main {

    public static void main(String[] args) {

        new Counter();

        new Counter();

        System.out.println(Counter.count); // Output: 2

    }

}

  • final
    • Class: Prevents inheritance (e.g., final class).
    • Method: Prevents overriding (e.g., final void).
    • Variable: Makes the value immutable (e.g., final int).

Example:

final class A {

    final int MAX = 100;

    final void display() {

        System.out.println(“This is a final method.”);

    }

}

  • abstract
    • Used to define incomplete classes or methods.
    • Example:

abstract class Shape {

    abstract void draw();

}

class Circle extends Shape {

    void draw() {

        System.out.println(“Drawing Circle”);

    }

}

  • synchronized
    • Ensures thread-safe execution.
    • Example:

synchronized void print() {

    System.out.println(“Thread-safe method”);

}

  • transient
    • Excludes variables from serialization.
    • Example:

class Demo implements Serializable {

    transient int temp; // Won’t be serialized

}


Usage Summary

Modifiers help you:

  • Define who can access your code (Access Modifiers).
  • Add specific behavior to variables, methods, or classes (Non-Access Modifiers).

Effective use of modifiers ensures a clean, secure, and robust code structure.