Use of transient

The word transient means ‘lasting for very short time’ and in Java it specifically mean to survive from the persistent state. As we know if any class is flagged as Serializable which means it will inform JVM that it can write Object of this Class in some external Stream or in short Object can be converted to binay representation.

But what if we don’t want some of our variables of class to be serialized ? Let’s consider following example for more.

class Customer implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String name;
	
	private String account;

	private /*transient*/ String securityCode;

	public String getName() {
		return name;
	}

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

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}
	public String getSecurityCode() {
		return securityCode;
	}

	public void setSecurityCode(String securityCode) {
		this.securityCode = securityCode;
	}
	
	@Override
	public String toString() {
		return name + ": " + account + " : " + securityCode ;
	}

}

Consider above class customer in which we have name, account number and one security code of the customer.
Now consider following class which is using above customer. Note currently we have commented the transient variable.

public class Transient {

	public static void main(String args[]) {
		ObjectOutputStream objectOutputStream = null;
		ObjectInputStream objectInputStream  = null;
		
		try {
			File file = new File("D://test.txt");
			FileOutputStream fileOutputStream = new FileOutputStream(file);
			
			objectOutputStream = new ObjectOutputStream(fileOutputStream);
			Customer customer = new Customer();
			customer.setAccount("123456007");
			customer.setName("James Bond");
			customer.setSecurityCode("JB007");
			
			objectOutputStream.writeObject(customer);//Write Object to test.txt
			
			FileInputStream fileInputStream = new FileInputStream(file);
			objectInputStream  = new ObjectInputStream(fileInputStream);
			Customer readObject = (Customer)objectInputStream.readObject();
			System.out.println(readObject);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				objectOutputStream.close();
				objectInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}

We have used one ‘test.txt’ file in which we have written the Object Customer with the help of ObjectOutputStream as this will write the Object in binary format which is not quite readable for us.So, we have used ObjetInputStream to read the same Serialized Object.Now run the Transient class and we will have following output.

James Bond: 123456007 : JB007

Yes we have successfully write Object to file and read the same from the file. But now we want to restrict Java to serialize security code from cutomer Object. It’s time to uncomment the transient variable from the Customer class.So, uncomment transient which means now security code is no longer be part of binary representation of Customer Object. Now run the Transient class and we will have following output.

James Bond: 123456007 : null

So, yes we stopped JVM from writing out field to some stream.

Variable marked transient means they are not part of persistent state of an Object which simply means skip some field from being Serialized.So, ultimately transient fields are kicked off when it’s time to persist the Object to some Stream if Object is Serializable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s