Java Modifier Types
Java Modifier Types
Section titled “Java Modifier Types”Modifiers are keywords in Java that you add to definitions (of classes, methods, variables, constructors) to change their meaning or behavior. They are broadly categorized into two groups:
- Access Modifiers: Control the visibility or accessibility level.
- Non-Access Modifiers: Provide other characteristics or functionalities.
Modifiers precede the rest of the definition:
// Access Modifiers:public class MyClass { ... }class PackagePrivateClass { ... } // Default access
public class AnotherClass { private String secretData; // Private access protected int inheritedValue; // Protected access public static final double PI = 3.14159; // Public access
public void publicMethod() { ... } private void helperMethod() { ... } protected void subclassMethod() { ... } void packageMethod() { ... } // Default access}
// Non-Access Modifiers:static final double WEEKS = 9.5;abstract class Shape { ... }final class ImmutableData { ... }
public static void main(String[] args) { ... }synchronized void updateCounter() { ... }volatile boolean flag;Access Control Modifiers
Section titled “Access Control Modifiers”Java provides four access levels:
public: Visible to all classes everywhere (in any package). Used for APIs intended for general use.protected: Visible within its own package and to subclasses (even if they are in different packages). Used for members intended for extension by subclasses.- Default (Package-Private): Visible only to classes within the same package. This is the access level when no explicit modifier (
public,protected,private) is used. Used for utility classes or members closely related within a package. private: Visible only within the declaring class itself. Not accessible by any other class, including subclasses. Used for implementation details and to enforce encapsulation.
Accessibility Summary:
| Modifier | Same Class | Same Package | Subclass (Diff Pkg) | Other Packages (World) |
|---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | Yes | No |
| Default | Yes | Yes | No | No |
private | Yes | No | No | No |
Note: Top-level classes (classes not nested inside another class) can only be public or default (package-private).
Non-Access Modifiers
Section titled “Non-Access Modifiers”These modifiers provide additional semantics:
static: * For variables (fields): Creates a class variable, shared by all instances of the class. Belongs to the class itself, not individual objects. Initialized when the class is loaded. * For methods: Creates a class method. Can be called directly on the class name (ClassName.staticMethod()) without creating an object. Cannot access instance variables or instance methods directly (only via an object reference). Cannot usethisorsuper.final: * For variables: Makes the variable a constant. Its value cannot be changed after initialization (either at declaration or in a constructor/static initializer). For reference variables, it means the reference cannot be changed to point to a different object, but the object itself might still be mutable. * For methods: Prevents the method from being overridden by subclasses. * For classes: Prevents the class from being subclassed (inherited from). Useful for creating immutable classes or preventing extension.abstract: * For classes: Declares a class that cannot be instantiated directly. It may or may not contain abstract methods. Designed to be subclassed. * For methods: Declares a method without an implementation (no body, ends with a semicolon). Must be declared within an abstract class. Subclasses must provide concrete implementations for all inherited abstract methods unless the subclass is also declared abstract.synchronized: Used for methods and blocks. Ensures that only one thread can execute a synchronized method or block on a given object instance (or class for static methods) at a time. Used for controlling access to shared resources in multithreaded applications.volatile: Used for variables. Indicates that the variable’s value might be changed by different threads. Guarantees that reads and writes to the volatile variable are atomic for reference types and most primitive types (exceptlonganddoublepre-Java 5) and ensures visibility of writes across threads.transient: Used for instance variables. Indicates that the variable should not be saved during object serialization.strictfp: (Less common now) Used for classes and methods. Ensures that floating-point calculations (float,double) adhere strictly to the IEEE 754 standard across all platforms, potentially sacrificing some performance for perfect reproducibility. Since Java 17, default floating-point semantics are implicitly strict.
What’s Next?
Section titled “What’s Next?”Having covered modifiers, the next chapter will delve into Basic Operators, the symbols used to perform operations like arithmetic, comparison, and logical evaluation in Java.