Java Modifiers

Java Modifiers

Modifiers in Java are keywords that are used to specify the accessibility, visibility, and behavior of classes, methods, variables, and other program elements. Java modifiers can be divided into two main categories: access modifiers and non-access modifiers.

Access Modifiers:

Access modifiers control the visibility or accessibility of classes, methods, and variables within a program.

  • public: The class, method, or variable is accessible from any other class.
  • protected: The class, method, or variable is accessible within its own package and subclasses.
  • default (no modifier): The class, method, or variable is accessible only within its own package.
  • private: The class, method, or variable is accessible only within its own class.
Abhiyantrik Tutorials image of for detail Explaination

Access modifiers in Java are keywords that determine the accessibility or scope of classes, methods, variables, and other members within a program. There are four main access modifiers in Java: private, default (no modifier), protected, and public. Each modifier has a specific scope and rules regarding where and how it can be accessed. Let's explore each of these access modifiers in detail:

Private Access Modifier:

  • The private access modifier restricts the accessibility of a class member (field, method, or inner class) to within the same class where it is declared.
  • Members declared as private cannot be accessed from outside the class, including from subclasses.
  • Useful for encapsulation and data hiding.
Example
class MyClass {
    private int privateVar;

    private void privateMethod() {
        // ...
    }
}

Default (No Modifier) Access Modifier:

  • When no access modifier is specified, the default access modifier is applied.
  • Members with default access are accessible within the same package, but not from outside the package.
  • Classes and members declared with default access have package-level visibility.
Example
class MyClass {
    int defaultVar; // Default access
}

Protected Access Modifier:

  • The protected access modifier allows class members to be accessed within the same package and by subclasses (even if they are in different packages).
  • Subclasses can access protected members of their superclass.
  • Useful for providing controlled access to subclasses.
Example
class Superclass {
    protected int protectedVar;

    protected void protectedMethod() {
        // ...
    }
}

class Subclass extends Superclass {
    void someMethod() {
        int value = protectedVar; // Accessing protected member
    }
}

Public Access Modifier:

  • The public access modifier allows class members to be accessed from anywhere, including from outside the class and package.
  • Members declared as public have the widest scope and can be accessed globally.
Example
public class MyClass {
    public int publicVar; // Public access

    public void publicMethod() {
        // ...
    }
}

Non-Access Modifiers:

Non-access modifiers do not control access but provide other functionality.

  • static: The method or variable belongs to the class rather than an instance of the class. It can be accessed using the class name.
  • final: The class cannot be extended (for classes), the method cannot be overridden (for methods), or the variable cannot be modified (for variables).
  • abstract: The class cannot be instantiated, and it must be subclassed. For methods, it indicates that the method has no implementation in the current class.
  • synchronized: The method or block is synchronized and can be accessed by only one thread at a time.
  • volatile: The variable may be modified by multiple threads, ensuring visibility of changes.
  • transient: The variable should not be serialized when the object is serialized.
  • strictfp: The method or class follows strict floating-point rules, ensuring platform-independent behavior for floating-point operations.
  • native: The method is implemented in platform-dependent code (e.g., in C or C++).
Access Modifiers Example:
public class MyClass { // Public class accessible from anywhere
    private int privateVar; // Private variable only accessible within this class
    protected int protectedVar; // Protected variable accessible within the package and subclasses
    int defaultVar; // Default variable accessible within the package
}
Non-Access Modifiers Example:
public class MyClass {
    static int staticVar; // Static variable belongs to the class, not instances
    final int constantVar = 42; // Final variable cannot be modified after initialization
    abstract void myMethod(); // Abstract method has no implementation in this class
    synchronized void syncMethod() { // Synchronized method accessed by one thread at a time
        // ...
    }
    transient int transientVar; // Transient variable not serialized when object is serialized
    volatile int volatileVar; // Volatile variable may be modified by multiple threads
    strictfp float calculate(float x) { // Strict floating-point calculation
        // ...
    }
    native void nativeMethod(); // Native method implemented in platform-dependent code
}

Modifiers in Java are essential for controlling access, ensuring security, and specifying behavior in object-oriented programming. Proper use of modifiers helps create well-structured and maintainable Java code.

Contact Us

Name
Email
Mobile No:
subject:
Message: