Previous | Home | Next |
In java applications, sometimes we will want to use a method that exists outside the JVM. In this scenario, the native modifier can help, native modifier can only apply to a method. Just as the abstract keyword, the native keyword indicates that the implementation of the method exists elsewhere.
In case of abstract, the implementation of a method may exist in a subclass of the class in which the abstract method is declared.
But in the case of native, the implementation of the method exists in a library outside the JVM. The native method is usually implemented in a non-Java 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:
System.loadLibrary("libraryName");
To declare a native method, precede the method with the native modifier, but do not define any body for the method.
Example
public native void Nativemethod() ;
After you declare a native method, you must write the native method and follow a complex series of steps to link it with your Java code. For example, the following code fragment presents an example of loading a library named NativeMethodDef which contains a method named NativeMethod():
Java Tutorialslass NativeModifierExample { native void NativeMethod(); static { System.loadLibrary("NativeMethodDef"); } }
Note:The library is loaded in a static code block which suggest that the library is loaded at the class load time, so it is there when a call to the native method is made. One can use the native method in the same way as we use a non-native method. For example, the following two lines of code would invoke the native method,
NativeModifierExample obj = new NativeModifierExample(); obj.NativeMethod();
In an java based application, while is in running mode, the objects are put in the random access memory (RAM) of the computer.That determines the scope and life of the object. However, if 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 serializable, the corresponding class must implement the interface Serializable. Thus, the transient modifier is related to storing an object in persistant 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.The transient modifier can be 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.
Example
Java Tutorialslass TransientModifier { transient int num1; // variable will not persist int num2; // variable will persist }
In java the volatile modifier only applies to instance variables just like the transient modifier. The variables declared volatile are subject to asynchronous modifications. Thus we can say that, declaring a variable volatile tells 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 generally used in multithreaded or multiprocessor environments. The volatile modifier tells the accessing thread that it should synchronize its private copy of the variable with the original copy in the memory. Consider the following example for the better understanding,
Example
Java Tutorialslass VolatileModifier extends Thread { private volatile static int someVal; // declared a volatile variable VolatileModifier(String str) { super(str); } public void run() // implementing the run method for threads { for (int j=0; j<7; j++) { try { System.out.println(getName() + "->"+i); if(getName().equals("r4r thread1")) { someVal=7; } if(getName().equals("r4r thread2")) { System.out.println("The value of volatile variable is :"+someVal); } Thread.sleep(2000); } Java Tutorialsatch(InterruptedException e) { e.printStackTrace(); } } } Java Tutorialslass VolatileModifierExample { public static void main(String args[]) { Thread t=new VolatileModifier("r4r thread1"); // creating a thread object Thread t1=new VolatileModifier("r4r thread2"); t.start(); // starting the execution of thread t1.start(); } }
The synchronized modifier is used in multithreaded programming to control access to critical sections in the program. The synchronized keyword used to indicate that a method can be accessed by only one thread at a time. The synchronized modifier is generally used mainly in threading environment. We will discuss about it in detail in threading. Its declarative syntax is,
public synchronized void Anymethod() { // some logic }
Previous | Home | Next |