MODIFIERS IN JAVA



What Is Modifier ?

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

Java Access Modifiers

Access modifiers specifies who can access them. There are three access modifiers and four access levels in java.

  • public
  • private
  • protected
  • no modifer i.e default

Class level access modifiers (java classes only)

Only two access modifiers is allowed, public and no modifier i.e default

  • If a class is public, then it can be accessed from all places.
  • If a class has no modifer, then it can only be accessed from same package.

Member level access modifiers (java variables and java methods)


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

Non Access Modifiers

  • Final
  • Abstract
  • Static
  • Strictfp
  • Native
  • Synchronized
  • Transient
Points To Remember
  • a method declaration can contain only one of the access modifiers public, protected and private
  • classes cannot be declared abstract and final simultaneously
  • abstract methods cannot be declared private, static, final, native, strictfp or synchronized
  • methods cannot be declared native and strictfp simultaneously
  • abstract and native methods have no body
  • abstract void method();
  • native void method();
  • a class that contains abstract method(s) must be declared abstract
  • final fields cannot be volatile

Java Modifier

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.