Modifiers are keywords that you add to those definitions to change their meanings. A modifier is a keyword that is placed before class, method or variable declaration that changes how it behaves.
In the Java programming language the following keywords are modifiers: abstract, final, native, private, protected, public, static, strictfp, synchronized, transient, and volatile.
The Java language has a wide variety of modifiers, including the following:
Java Access Modifiers
Non Access Modifiers
Access modifiers specifies who can access them. There are three access modifiers and four access levels in java.
Only two access modifiers is allowed, public and no modifier i.e default
Access Modifiers
|
Same Class | Same Package | Subclass | Other packages |
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
no access modifier | Y | Y | N | N |
private | Y | N | N | N |
Modifier | Used on | Meaning |
abstract | class interface method |
Contains unimplemented methods and cannot be instantiated. All interfaces are abstract. Optional in declarations No body, only signature. The enclosing class is abstract |
final |
class method field variable |
Cannot be subclassed Cannot be overridden and dynamically looked up Cannot change its value. static final fields are compile-time constants. Cannot change its value. |
native |
method | Platform-dependent.
No body, only signature |
none(package) |
class interface member |
Accessible only in its package Accessible only in its package Accessible only in its package |
private |
member |
Accessible only in its class(which defins it). |
protected |
member |
Accessible only within its package and its subclasses |
public |
class interface member |
Accessible anywhere Accessible anywhere Accessible anywhere its class is. |
strictfp |
class method |
All methods in the class are implicitly strictfp. All floating-point computation done is strictly conforms to the IEEE 754 standard. All values including intermediate results must be expressed as IEEE float or double values. It is rarely used. |
static |
class method field initializer |
Make an inner class top-level class A class method, invoked through the class name. A class field, invoked through the class name one instance, regardless of class instances created. Run when the class is loaded, rather than when an instance is created. |
synchronized |
method |
For a static method, a lock for the class is acquired before executing the method. For a non-static method, a lock for the specific object instance is acquired. |
transient |
field |
Not be serialized with the object, used with object serializations. |
volatile |
field |
Accessible by unsynchronized threads, very rarely used. |