Non-Access Modifiers in Java – DEV Community

Non-Access Modifiers in Java – DEV Community


Non-access modifiers in Java control various aspects of classes, methods, and variables beyond just their visibility. Unlike access modifiers (public, private, protected), these modifiers don’t control access levels but instead provide other functionality.



Main Non-Access Modifiers



1. static

  • Makes a member belong to the class rather than instances
  • Can be applied to:
    • Variables (class variables)
    • Methods (class methods)
    • Nested classes
    • Initializer blocks
class Counter {
    static int count = 0; // Class variable

    static void increment() { // Class method
        count++;
    }
}
Enter fullscreen mode

Exit fullscreen mode



2. final

  • Makes elements unchangeable after initialization
  • Can be applied to:
    • Classes (cannot be extended)
    • Methods (cannot be overridden)
    • Variables (cannot be reassigned)
final class Constants {
    final double PI = 3.14159;

    final void display() {
        System.out.println("This cannot be overridden");
    }
}
Enter fullscreen mode

Exit fullscreen mode



3. abstract

  • Used for incomplete implementations
  • Can be applied to:
    • Classes (cannot be instantiated)
    • Methods (no body, must be implemented by subclasses)
abstract class Shape {
    abstract double area(); // Abstract method

    void display() {
        System.out.println("This is a shape");
    }
}
Enter fullscreen mode

Exit fullscreen mode



4. synchronized

  • Controls thread access to methods/blocks
  • Ensures only one thread can execute at a time
class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }
}
Enter fullscreen mode

Exit fullscreen mode



5. transient

  • Marks variables not to be serialized
  • Used with object serialization
class User implements Serializable {
    String username;
    transient String password; // Won't be serialized
}
Enter fullscreen mode

Exit fullscreen mode



6. volatile

  • Ensures variable is always read from/written to main memory
  • Used in multithreading environments
class SharedObject {
    volatile boolean flag = false;
}
Enter fullscreen mode

Exit fullscreen mode



7. native

  • Indicates a method is implemented in platform-dependent code (like C)
  • Used with Java Native Interface (JNI)
public native void nativeMethod();
Enter fullscreen mode

Exit fullscreen mode



8. strictfp

  • Ensures consistent floating-point calculations across platforms
  • Can be applied to classes, interfaces, and methods
strictfp class Calculator {
    strictfp double calculate() {
        return 10.0/3.0;
    }
}
Enter fullscreen mode

Exit fullscreen mode



Usage Guidelines

Modifier Applies To Purpose
static Variables, methods, nested classes, blocks Class-level rather than instance-level
final Classes, methods, variables Prevent modification/extension
abstract Classes, methods Incomplete implementations
synchronized Methods, blocks Thread safety
transient Variables Exclude from serialization
volatile Variables Visibility across threads
native Methods Platform-native implementation
strictfp Classes, interfaces, methods Consistent floating-point



Important Notes

  1. Some modifiers cannot be combined:

    • abstract + final (contradictory)
    • abstract + private (private methods can’t be overridden)
    • abstract + static (static methods can’t be overridden)
  2. Method-specific modifiers:

    • synchronized, native, strictfp are only for methods
  3. Variable-specific modifiers:

    • transient, volatile are only for variables

Understanding these non-access modifiers is essential for writing correct, efficient, and maintainable Java code that properly leverages language features.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *