Skip to main content

2. Java Modifiers

Java modifiers are keywords that you use to change the behavior and accessibility of classes, methods, and variables. They are classified into two categories: Access Modifiers and Non-Access Modifiers.

1. Access Modifiers

Access modifiers control the visibility or accessibility of classes, methods, and variables. There are four types:

ModifierClassPackageSubclass (same package)Subclass (different package)World
publicYesYesYesYesYes
protectedYesYesYesYes (with inheritance)No
no modifier (package-private)YesYesYesNoNo
privateYesNoNoNoNo

Access Modifiers Explained

  • public: Accessible from anywhere in the program (any package or class).
  • protected: Accessible within the same package and by subclasses in other packages.
  • no modifier (package-private): Accessible within the same package only.
  • private: Accessible only within the same class.

Example

public class Example {
private int id; // Private field, not accessible outside this class
protected String name; // Protected field, accessible within package and subclasses
public void display() {
System.out.println(id + " " + name);
}
}

2. Non-Access Modifiers

Non-access modifiers control aspects like behavior, inheritance, and concurrency. Common non-access modifiers include:

2.1. final

  • Applied to classes, methods, and variables.
  • When applied to:
    • Class: Prevents the class from being subclassed.
    • Method: Prevents the method from being overridden.
    • Variable: Makes the variable a constant (its value cannot be changed after initialization).

Example:

public final class FinalClass {
// This class cannot be subclassed
}

public class Example {
public final void display() {
// This method cannot be overridden in subclasses
}

public final int MAX_VALUE = 100; // Constant variable
}

2.2. static

  • Applied to methods, fields, and nested classes.
  • Static members belong to the class rather than an instance, meaning they can be accessed without creating an object.
public class Example {
public static int count = 0; // Static field

public static void displayCount() { // Static method
System.out.println(count);
}
}

2.3. abstract

  • Applied to classes and methods.
  • An abstract class cannot be instantiated and must be subclassed.
  • An abstract method must be implemented by any subclass.
public abstract class Animal {
public abstract void sound(); // Abstract method
}

public class Dog extends Animal {
@Override
public void sound() {
System.out.println("Bark");
}
}

2.4. synchronized

  • Applied to methods and blocks.
  • Ensures that a method or block is only accessed by one thread at a time (thread safety).
public class Example {
public synchronized void display() {
// Only one thread can access this method at a time
}
}

2.5. volatile

  • Applied to variables.
  • Ensures that the value of the variable is always read from memory, not from a thread's local cache.
public class Example {
private volatile boolean isActive;
}

2.6. transient

  • Applied to fields.
  • Prevents the field from being serialized (i.e., it won't be included in the serialization process).
public class Example implements Serializable {
private transient int tempData; // This field won't be serialized
}

2.7. native

  • Applied to methods.
  • Indicates that the method is implemented in platform-dependent code (e.g., C or C++) using JNI (Java Native Interface).
public class Example {
public native void nativeMethod(); // Native method
}

2.8. strictfp

  • Applied to classes and methods.
  • Ensures that floating-point calculations follow strict IEEE 754 standards for portability across platforms.
public strictfp class Example {
public strictfp void calculate() {
// Floating-point calculations follow strict precision rules
}
}

Summary of Non-Access Modifiers

ModifierApplies ToDescription
finalClass, Method, VariablePrevents inheritance, overriding, or reassignment.
staticMethod, Field, ClassBelongs to the class rather than an instance.
abstractClass, MethodRequires subclasses to implement abstract methods.
synchronizedMethod, BlockEnsures thread safety by allowing one thread at a time.
volatileVariableGuarantees the variable is read from memory, not cache.
transientVariableExcludes the field from serialization.
nativeMethodMethod is implemented in platform-specific code.
strictfpClass, MethodEnforces strict floating-point calculations.

Example: Combining Access and Non-Access Modifiers

public abstract class Vehicle {
private static final int MAX_SPEED = 120; // Constant field

public abstract void startEngine(); // Abstract method

public final void stopEngine() {
System.out.println("Engine stopped."); // Final method, cannot be overridden
}
}