Which class is super class of every class

What is volatile and transient variable in java

OR
What is volatile variable how it is use in java. Also tell me the details of transient variables

Answer : In java language two type modifiers: transient and volatile defined to handle specialized situations.

transient: Whenever any instance variable declared transient, its value will not be persists when we store the object. See example class below:

class ValueToSave implements Serializable{

	private static final long serialVersionUID = 1L;
	// will not persist
	transient String valueNotSerialized ;
	// will persist
	String name = "Java Honk";
}

 

Let’s create full example to test transient variable. Please have it below:

package com.javahonk.transienttest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TransientTest {

	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {

	ObjectOutputStream oos = null;
	FileInputStream fin = null;
	ObjectInputStream ois = null;
	try {

		// Create file first
		File file = new File("C:\\JavaHonk\\Test.txt");

		if (file.createNewFile()) {
			System.out.println("File created!");
		} else {
			System.out.println("File exists");
		}

		// Write object to file
	    FileOutputStream fout = new FileOutputStream(file);
	    oos = new ObjectOutputStream(fout);
	    ValueToSave valueToSave = new ValueToSave();
	    valueToSave.setValueNotSerialized("Transient Check");
	    valueToSave.setName("Java Honk");
	    oos.writeObject(valueToSave);
	    System.out.println("Object sucessfully written");

	    // Read from file
	    fin = new FileInputStream("C:\\JavaHonk\\Test.txt");
	    ois = new ObjectInputStream(fin);
	    ValueToSave valueToSave2 = (ValueToSave) ois.readObject();
	    System.out.println("Content retrieved: "+
	    		valueToSave2.getName());
	    System.out.println("Transient value retrieved: "+
	    		valueToSave2.getValueNotSerialized());

	} catch (IOException e) {
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} finally {
		try {
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	}

}

class ValueToSave implements Serializable{

	private static final long serialVersionUID = 1L;
	// will not persist
	transient String valueNotSerialized ;
	// will persist
	String name = "Java Honk";

	public String getValueNotSerialized() {
		return valueNotSerialized;
	}

	public void setValueNotSerialized(String valueNotSerialized) {
		this.valueNotSerialized = valueNotSerialized;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

 

Output:

What is volatile and transient variable in java

 

Volatile: In java volatile modifiers is always uncertain and it can be changed unexpectedly in others part of the code. Good example is multithreaded programs where two or more threads share the same variable and each thread could keep its own copy and it could be private copy of a shared variable. The master copy of variable can be updated multiple times such as when it enters to synchronized methods. This approach and thought was good but It can be inefficient at times. All that really matter is master copy of the variable continuously should reflects its current state. To ensure this specify the variable as volatile which will tell the compiler that it should always use master copy of volatile variable (always keep private copies up-to-date with the master copy, and vice versa) and access to master variable should be executed in the precise order where they are executed on any private copy.

Leave a Reply

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