Java Non-Access Modifiers or Non-Access Controls

Java non-access modifiers do not control access level, but provides other functionality like:

  • to control inheritance capabilities,
  • to check whether all objects of the class have their own values or share the same member value,
  • to check whether a method can be overridden in a subclass.

Java non-access modifiers are categorised separately for classes & attributes and methods level as follows:

For class:
A class can be final:
Final class cannot be inherited by other classes or we can also say, output of final class can not be overriden.

A class can be abstract:
If a class is defined as abstract then it cannot be used to create objects or in other words, in order to access an abstract class, it must be inherited from another class defined in same program of outside of program using inheritance.

For attributes and methods:
final:
Attributes values can’t be changed once assigned and methods cannot be overridden.

static:
Attributes and/or methods belongs to the class, rather than an object of that class.

abstract:
Can only be used in an abstract class, and can only be used on methods. If a method is defined abstract, it has to be implemented in a subclass, which contains abstract methods (a process of abstraction and inheritance).

transient:
Attributes and/or methods are skipped when serializing an object containing them.

synchronized:
synchronized controls thread access to a block/method. Methods can only be accessed by one thread at a time.

volatile:
Attribute’s value is not cached thread-locally, and is always read from the “main memory” (not from a specific thread’s memory).

native:
native is a simple keyword that marks a method that will be implemented in other languages, not in Java. It works together with the Java Native Interface (JNI).

In short, we have only 6 working non-access modifiers in Java: static, final, abstract, synchronized, volatile and transient.

See Java Access Modifiers or Access Controls

One Reply to “Java Non-Access Modifiers or Non-Access Controls”

Leave a Reply

Your email address will not be published. Required fields are marked *