Some Important Modifiers in Java


In this article, we are going to describe some important modifiers which are used with method data members, etc. We also describe separate examples of each modifier. In Java, there are many modifiers used for various purposes. The following list gives all the modifiers. In the following table, some of the modifiers are used very commonly in all languages such as public, protected, static, volatile, etc. And most of you know about the use of these common modifiers. So now we describe only three of the modifiers; native, transient and volatile.
Java Modifier Name and Description Table
Modifier Name Used on  Description
public class
Accessible anywhere
private member Accessibility only in its class (which defines it).
protected member Accessibility only within its package and its subclasses
Accessible only in its package
final class
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.
abstract class
Contains unimplemented methods and cannot be instantiated.
All interfaces are abstract. Optional in declarations
Nobody, only signature. The enclosing class is abstract
static class
Make an inner class a 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.
strictfp class
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.
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.
volatile field Accessible by unsynchronized threads, very rarely used.
transient field Not be serialized with the object, used with object serializations.
native member Accessible only in its class(which defines it).

Native Modifier

The native modifier is used where sometimes we want to use a method that exists outside the JVM. The native keyword indicates that. And this modifier is only applicable to methods. Like the abstract keyword, the native keyword shows that the implementation of this method exists elsewhere. And the native method implementation exists in a library outside of the JVM. All the native methods are implemented in another language such as C or C++. Before a native method can be invoked, a library that contains the method must be loaded and that library is loaded by making the following system call.
Note-  System.loadLibrary("libraryName");
This line is used to load the library libraryName loadLibrary() is the method of the System class and this line is written in a static block because all the static blocks are loaded at class loading time. This library will load first of requesting by another run the method of this library.
public class NativeDemo
   native voidNativeMethod();

Transient Modifier

In the Java technology, while any Java program is in a running state, the objects are put in the RAM of the System. That determines the scope and life of the object. However an object may be stored in persistent storage outside the JVM, for later use by the same application or by some another application. The process of storing an object is called serialization. For an object to be serialized, the corresponding class must implement the interface Serialization. Thus, the transient modifier is related to storing an object in persistent storage. Such storage is called the object's persistent state. A variable declared transient is not stored, and hence does not become part of the object's persistent state. So a transient modifier is used to prevent a security-sensitive piece of data from copying to a source where there is no security mechanism exists. 
The transient modifier can only be applied to instance variables. When you are declaring an instance variable transient, you are telling the Java Virtual Machine not to store this variable when the object, in which it is declared, is serialized. Thus, when an instance variable is declared as transient, then its value need not persist when an object is stored.
public classTransientDemo implementsSerializable
      // variable will not persist
transient int num1;
      // variable will persist
      int num2;

Volatile Modifier

In the Java language, the volatile modifier only applies to instance variables just such as the transient modifier. Those variables declared volatile are part of asynchronous modifications. Thus we can say that, declaring a variable volatile indicates to the compiler that this variable might be changed unexpectedly by other parts of the program. So, the compiler takes some special precautions to keep this variable properly updated; volatile variables are commonly used in multithreaded applications or multiprocessor environments. The volatile modifier indicates to the thread which accesses the volatile member that it should synchronize its private copy of the variable with the original copy in the memory. Consider the following example for a better understanding.
  1. class VolatileDemo extends Thread {  
  2.  // declared a volatile variable  
  3.  private volatile static int someVal;  
  4.  VolatileDemo(String str) {  
  5.   super(str);  
  6.  }  
  7.  // implementing the run method for threads  
  8.  public void run() {  
  9.   for (int j = 0; j < 7; j++)  
  10.   {  
  11.    try {  
  12.     System.out.println(getName() + "->" + j);  
  14.     if (getName().equals("Abhishek thread 1"))  
  15.     {  
  16.      someVal = 101;  
  17.     }  
  18.     if (getName().equals("Abhishek thread 2"))  
  19.     {  
  20.      System.out.println("The value of volatile variable is :" + someVal);  
  21.     }  
  22.     Thread.sleep(1000);  
  23.    } catch (InterruptedException e) {  
  24.     e.printStackTrace();  
  25.    }  
  26.   }  
  27.  }  
  28. }  
  29. public class VolatileModifierDemo {  
  30.  public void main(String args[]) {  
  31.   // creating a thread object  
  32.   Thread t1 = new VolatileDemo("Abhishek thread 1");  
  33.   // starting the execution of thread      
  34.   Thread t2 = new VolatileDemo("Abhishek thread 2");  
  35.   t1.start();  
  36.   t2.start();  
  37.  }  
  38. }