๐Ÿงฑ Java Object-Oriented Programming
Estimated reading: 4 minutes 341 views

Java Modifiers โ€“ Complete Guide with Syntax, Examples & Best Practices


Introduction โ€“ Why Java Modifiers Are Essential

In Java, modifiers are like access badges or behavior tags that you assign to classes, methods, and variables. They define who can access what and how certain features behave.

From securing data to enabling inheritance, Java modifiers are at the core of encapsulation, inheritance, and code architecture.

By the end of this guide, youโ€™ll understand:

Types of Java modifiers: access and non-access
How to use public, private, static, final, abstract, and more
Real-world examples to apply each modifier effectively
Best practices for clean, maintainable, and secure code


Types of Java Modifiers

Java provides two main types of modifiers:

Modifier TypeExamplesPurpose
Access Modifierspublic, private, protected, (default)Control visibility of classes/members
Non-access Modifiersstatic, final, abstract, synchronized, native, transient, volatile, strictfpDefine behavior or restrictions

Access Modifiers in Java

public Modifier

public class Car {
    public String brand = "Tesla";

    public void display() {
        System.out.println("Brand: " + brand);
    }
}
  • Accessible from any other class
  • Used for API methods, main(), and reusable classes

private Modifier

public class Engine {
    private int rpm = 7000;

    private void start() {
        System.out.println("Engine started");
    }
}
  • Accessible only within the same class
  • Great for data hiding and encapsulation

protected Modifier

class Vehicle {
    protected int speed = 100;
}
class Bike extends Vehicle {
    public void showSpeed() {
        System.out.println("Speed: " + speed);
    }
}
  • Accessible in the same package and subclasses
  • Useful in inheritance scenarios

Default (No Modifier)

If no access modifier is specified, the default modifier is applied:

class Person {
    String name = "John";
}
  • Accessible only within the same package
  • Not visible from outside the package

Non-Access Modifiers in Java

static Modifier

public class MathUtils {
    static int square(int n) {
        return n * n;
    }
}
  • Belongs to the class rather than instances
  • Can be called without creating an object

final Modifier

final class Constants {
    final int MAX = 100;
}
  • Prevents modification:
    • Final class canโ€™t be extended
    • Final method canโ€™t be overridden
    • Final variable canโ€™t be reassigned

abstract Modifier

abstract class Animal {
    abstract void sound();
}
  • Cannot be instantiated
  • Used for incomplete base classes
  • Forces subclasses to implement abstract methods

synchronized Modifier

public synchronized void increment() {
    count++;
}
  • Ensures thread safety for methods or blocks
  • Used in multithreaded environments

native Modifier

public native void nativeMethod();
  • Specifies a method implemented in platform-dependent code (e.g., C/C++)

transient Modifier

transient int tempValue;
  • Excludes a field from serialization
  • Useful when persisting objects to files

volatile Modifier

volatile boolean flag = true;
  • Guarantees visibility of changes to variables across threads
  • Used in concurrent programming

strictfp Modifier

strictfp class Calculator {
    strictfp double compute() {
        return 10.0 / 3.0;
    }
}
  • Ensures floating-point consistency across platforms

Modifier Comparison Table

ModifierApplies ToPurposeInheritance / Usage
publicClass, Method, VariableVisible everywhereYes
privateMethod, VariableOnly in same classNo
protectedMethod, VariableSame package + subclassesYes
staticVariable, Method, BlockBelongs to classNot instance-bound
finalClass, Method, VariablePrevent changesNo override/extend
abstractClass, MethodMust be subclassedNeeds implementation
synchronizedMethod, BlockThread safetyUsed with threads
nativeMethodNon-Java implementationPlatform-dependent
transientVariableSkip during serializationSerialization context
volatileVariableShared memory visibility in threadsMultithreaded env.
strictfpClass, MethodFloating-point precision consistencyJVM portability

Best Practices for Java Modifiers

Use private for data hiding and maintainability
Apply final for constants and secure inheritance
Use static for shared utilities and performance
Prefer protected only when subclassing is required
Donโ€™t overuse publicโ€”it increases tight coupling


Summary

  • Java modifiers define access control and behavior
  • Mastering both access and non-access modifiers is key to writing clean, efficient, and secure code
  • Each modifier has specific use cases โ€” knowing when and how to use them helps create robust Java applications

FAQs โ€“ Java Modifiers

What is the difference between private and protected?

private restricts access to the same class, while protected allows access from the same package and child classes.

Can we use multiple modifiers together?

Yes. For example:

private static final int MAX = 100;

What does transient mean in Java?

transient is used to skip fields during serialization, making them non-persistent.

What happens if no access modifier is used?

The member or class gets default (package-private) access โ€” only accessible within the same package.

Can a constructor be static?

No. Constructors are not static because they are used to create objects.


Share Now :
Share

Java Modifiers

Or Copy Link

CONTENTS
Scroll to Top