Modifier

Author: Ravi Poswal

Modifiers are keywords that alter the properties, behavior, or accessibility of constructors, variables, methods, and classes. A Modifier is the accessibility of the variable, methods, and classes, which is divided into two categories.

  1. Access modifier
  2. Non-access modifier

1. Access Modifier:

This shows the class member's control access level, and instance member.java provide four access level

S.No

Access modifier

Details

1

Default

Any other class contained in the package has access to the class. It is no need to specify the keyword.

2

Public

The code is accessible anywhere

3

Private

For the variable method, which is used only with in class

4

Protected

This keyword is used within packages and subclasses

 

2. Non-Access Modifier 

It is used for many others functionally.

S.NO

MODIFIER

DETAILS

1

Final

Used to declare constants or prevent inheritance and overriding.

2

Static

showing to the class member

3

Synchronized

This modifier is used to access a single thread at a time

4

Volatile

The volatile modifier is not cache local thread. It is always read from the main memory

 

FAQs

Modifiers in Java are keywords used to change the behavior, visibility, or accessibility of classes, methods, and variables. They help control how program elements can be accessed and used.

There are two main types of modifiers in Java:

Access Modifiers
Non-Access Modifiers

Access modifiers in Java control the accessibility or visibility of classes, methods, and variables. The four access modifiers are:

public
private
protected
default

Non-access modifiers provide additional functionality to classes, methods, and variables instead of controlling access. Common non-access modifiers include:

static
final
abstract
synchronized
volatile

The final modifier is used to restrict changes.

A final variable cannot be reassigned.
A final method cannot be overridden.
A final class cannot be inherited.

Yes, Java allows multiple modifiers together as long as they are compatible.

public static final int MAX_VALUE = 100;