1.

What Are Transient Variables? What Role Do They Play In Serialization Process?

Answer»

The TRANSIENT keyword in Java is used to indicate that a field should not be serialized. Once the process of de-serialization is carried out, the transient variables do not UNDERGO a change and retain their default value. Marking unwanted fields as transient can help you boost the serialization PERFORMANCE. Below is a simple example where you can see the use of transient keyword.

 class MyVideo IMPLEMENTS Serializable

{

private VIDEO video;

private transient Image thumbnailVideo;

private void generateThumbnail()

{

// Generate thumbnail.

}

 private void readObject(ObjectInputStream inputStream)

throws IOException, ClassNotFoundException

{

inputStream.defaultReadObject();

generateThumbnail();

}

}

The transient keyword in Java is used to indicate that a field should not be serialized. Once the process of de-serialization is carried out, the transient variables do not undergo a change and retain their default value. Marking unwanted fields as transient can help you boost the serialization performance. Below is a simple example where you can see the use of transient keyword.

 class MyVideo implements Serializable

{

private Video video;

private transient Image thumbnailVideo;

private void generateThumbnail()

{

// Generate thumbnail.

}

 private void readObject(ObjectInputStream inputStream)

throws IOException, ClassNotFoundException

{

inputStream.defaultReadObject();

generateThumbnail();

}

}



Discussion

No Comment Found