0
Sponsored Links


Ad by Google
The transient keyword is one of the important keyword in java to understand. Well most of the time interviewer, asked you about the transient keyword, at the time of asking question about the Serialization. Before going to see the use of transient keyword you must have to good understanding of Serialization in java. Here is tutorial on Serialization.

Well transient keyword is used while Serialization of an object. Serialization is a techniques to convert your object's states into sequence of byte, so that you can easily transfer these object via network from source to destination, for example one JVM to another JVM.

The transient keyword is only apply with fields(member variable of your class) not with method or class. While serialization sometimes you don't want to serialized a particular member variable of your class. For example say you have an object KingMaker with member variable threadA and owner, at the time of serialization, you don't want to serialized the value of threadA variable, then you must have to mark the variable threadA as transient, all the non serializable member variables must marked with transient keyword.

Note: System-level classes such as Thread, OutputStream and its subclasses, and Socket are not serializable, so mark them as transient.

The field(s) which are marked as transient can not be serialized and at the time of deserialization they will assigned with the default value of the declared data types, for example
  • primitive data types 0 or false
  • and for reference data types type null.

Let's see the implementation of transient keyword.

SerializableObject.java, having three member variables, two of three declared with transient keyword.

package com.javamakeuse.poc;

import java.io.Serializable;

public class SerializableObject implements Serializable {

 private String secretString;
 private transient boolean serialized;
 private transient int noOfObject;

 public SerializableObject(String secretString, boolean serialized,
   int noOfObject) {
  super();
  this.secretString = secretString;
  this.serialized = serialized;
  this.noOfObject = noOfObject;
 }

 public String getSecretString() {
  return secretString;
 }

 public boolean isSerialized() {
  return serialized;
 }

 public int getNoOfObject() {
  return noOfObject;
 }

}

TransientTest.java class, to serialize an object and restore the object from the serialized stream.

package com.javamakeuse.poc;

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

public class TransientTest {

 private static String file = "tran.ser";

 private static void serialized(Object obj) throws IOException {
  FileOutputStream fos = new FileOutputStream(file);
  ObjectOutputStream out = new ObjectOutputStream(fos);
  out.writeObject(obj);
  out.close();
 }

 private static SerializableObject deserialized() throws IOException,
   ClassNotFoundException {
  FileInputStream fis = new FileInputStream(file);
  ObjectInputStream in = new ObjectInputStream(fis);
  return (SerializableObject) in.readObject();

 }

 public static void main(String[] args) {
  try {
   // serializing an object
   serialized(new SerializableObject("secret", true, 1));

   // restoring the object from the serialized stream.
   SerializableObject obj = deserialized();
   System.out.println("After deserialization value of secretString = "
     + obj.getSecretString());
   System.out.println("After deserialization value of serialized = "
     + obj.isSerialized());
   System.out.println("After deserialization value of noOfObject = "
     + obj.getNoOfObject());

  } catch (IOException | ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

Run the TransientTest.java class and see the output, it will assigned the default values for the transient fields.

OUTPUT:
After deserialization value of secretString = secret
After deserialization value of serialized = false
After deserialization value of noOfObject = 0


References:
Reference 1

That's it.
Sponsored Links

0 comments:

Post a Comment