Java Modifiers

In Java programming language, a modifier is a keyword that is used to specify the properties of a class, method, or variable.

Modifiers in Java determine the accessibility, visibility, and functionalities of class members.

There are two types of modifiers in Java: access modifiers and non-access modifiers.

Access Modifiers

Access modifiers are used to control the accessibility of a class, method, or variable.

There are four access modifiers in Java: public, protected, default (no modifier), and private.

Public Modifier

The public modifier makes a class, method, or variable accessible from any part of the code.

A class, method, or variable declared with the public modifier can be accessed from any other class, regardless of the package it belongs to.

Example:

public class SampleClass {
    public int x;
    public void sampleMethod() {
        System.out.println("This is a public method.");
    }
}

Protected Modifier

The protected modifier makes a class, method, or variable accessible within the same package or by a subclass in a different package.

Example:

protected class SampleClass {
    protected int x;
    protected void sampleMethod() {
        System.out.println("This is a protected method.");
    }
}

Default Modifier (No Modifier)

When no access modifier is specified, it is considered as the default access modifier.

The default access modifier makes a class, method, or variable accessible within the same package but not from outside the package.

Example:

class SampleClass {
    int x;
    void sampleMethod() {
        System.out.println("This is a default method.");
    }
}

Private Modifier

The private modifier makes a class, method, or variable accessible only within the same class.

A private class, method, or variable cannot be accessed from outside the class.

Example:

class SampleClass {
    private int x;
    private void sampleMethod() {
        System.out.println("This is a private method.");
    }
}

Non-Access Modifiers

Non-access modifiers are used to specify additional properties of a class, method, or variable.

There are several non-access modifiers in Java, including static, final, abstract, synchronized, native, strictfp, and transient.

Static Modifier

The static modifier is used to declare a class member that belongs to the class rather than to an instance of the class.

A static member can be accessed without creating an instance of the class.

Example:

class SampleClass {
    static int x;
    static void sampleMethod() {
        System.out.println("This is a static method.");
    }
}

Final Modifier

The final modifier is used to declare a class, method, or variable that cannot be modified.

A final class cannot be subclassed, a final method cannot be overridden, and a final variable cannot be changed after it is assigned a value.

Example:

final class SampleClass {
    final int x = 10;
    final void sampleMethod() {
        System.out.println("This is a final method.");
    }
}

Abstract Modifier

The abstract modifier is used to declare an abstract class or method.

An abstract class cannot be instantiated and is used as a base class for other classes.

An abstract method is a method without an implementation and must be overridden by a subclass.

Example:

abstract class SampleClass {
    int x;
    abstract void sampleMethod();
}

class SubClass extends SampleClass {
    @Override
    void sampleMethod() {
        System.out.println("This is an overridden abstract method.");
    }
}

Synchronized Modifier

The synchronized modifier is used to declare a synchronized method.

A synchronized method is used to control access to a method by multiple threads.

Only one thread can execute a synchronized method at a time.

Example:

class SampleClass {
    int x;
    synchronized void sampleMethod() {
        System.out.println("This is a synchronized method.");
    }
}

Native Modifier

The native modifier is used to declare a native method.

A native method is a method that is implemented in a language other than Java.

Example:

class SampleClass {
    native void sampleMethod();
}

Strictfp Modifier

The strictfp modifier is used to declare a strict floating-point method.

A strict floating-point method ensures that the result of the method is the same on all platforms.

Example:

strictfp class SampleClass {
    int x;
    strictfp void sampleMethod() {
        System.out.println("This is a strictfp method.");
    }
}

Transient Modifier

The transient modifier is used to declare a transient variable.

A transient variable is a variable that will not be serialized when an object is persisted.

Example:

class SampleClass implements Serializable {
    private int x;
    private transient int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}

Conclusion

In Java, modifiers are used to control the accessibility, visibility, and functionalities of class members.

There are four access modifiers in Java (public, protected, default, and private) and several non-access modifiers (static, final, abstract, synchronized, native, strictfp, and transient).

Understanding and using the correct modifier is important for writing efficient and maintainable Java code.

Java Basics