Friday 26 July 2013

Native, Transient, Synchronized, Volatile Modifiers in java

// siddhu vydyabhushana // 3 comments
Native Modifier:  

This is used as a methods modifier. Its body is implemented in another programming language such as c (or) c++. Native methods are platform dependent. The Native modifier informs the java compiler that a methods implementation is in external c file. It is for this reason that native method declaration look different from other java methods .they have no body
                
                                   E.g.:  native int findTotal ();


Note:   That the method declaration ends with a semicolon. There are no curly braces containing java code .This is implemented in C or C++ code. It  makes potential security risk and loss of portability
Transient Modifier

The object has a transient part it is not part of the persistent state of an object. You can use the transient modifier if you do not want to store certain data member to file. This is used only with data members.

Class transistent
{
   Transistent Boolean b; //not to be stored
   Int k;                                 //to be stored
} 

Volatile Modifier

It may be modified by asynchronous threads. The volatile modifier is used for volatile that can be simultaneously modified by many threads.

Note:
      1.     You can change the order of the access specifier and modifiers
                                Public static void main(String args[])
                                Static public void main(String srgs[])

2.      You cannot use two or  more access specifiers in a declaration
Private public int a; //illegal

Synchronized Modifier


This is used in multi –threaded programming .A thread is a unit of execution within a process. In multi-threaded you need to synchronize various threads. The synchronized keyword used to tell the program that the thread is safe. This is allowing a single thread access to a method at once, forcing the others to wait their turn.

3 comments: