Java transient keyword with example

Transient variable is the variable whose value is not serialized during serialization. You will get default value for these variable when you deserialize it.
Lets say you have Country class and you don’t want to Serialize population attribute as it will change with time, so you can declare population attribute as transient and it won’t serialized any more.

Java Serialization Tutorial:

Transient keyword example:

Create a classed called Country.java as below:
Create serializationMain.java as below:
When you run above program, you will get below output:
Now Create a classed called DeserializeMain.java as below:
When you run above program, you will get below output:
As you can see in above example, we have declared population as trasient,so after deserialization, its value became 0 (Default value for long)

Related Posts

  • Serialization interview questions
    14 February

    Java Serialization interview questions and answers

    Serialization is one of most important concept in java. If you are going to face core java interview, then you might be asked some questions from Serialization. Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serialization externalizable in java Transient keyword in java Difference between Serializable and Externalizable […]

  • 14 February

    Difference between Serializable and Externalizable in Java

    Table of ContentsJava Serialization Tutorial:Externalizable vs Serializable: In this tutorial,  we are going to see differences between Serializable and Externalizable interface in Java. Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serialization externalizable in java Transient keyword in java Difference between Serializable and Externalizable in Java Before understanding difference, let […]

  • 26 February

    Externalizable in java

    Table of ContentsJava Serialization Tutorial:Externalizable: This is 3rd part of java Serialization tutorial Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serializationexternalizable in java Transient keyword in java Difference between Serializable and Externalizable in Java Before understanding Externalizable interface, you need to have idea about Serialization.You can read […]

  • 11 March

    serialVersionUID in java Serialization

    This is 2nd part of java Serialization tutorial Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serialization externalizable in java Transient keyword in java Difference between Serializable and Externalizable in Java serialVersionUID is used to ensure that same class(That was used during Serialization) is loaded during Deserialization.serialVersionUID is […]

  • 09 March

    Serialization in java

    Table of ContentsJava Serialization Tutorial:Need of Serialization in java? Concept of serialVersionUID :For Deserialization:Summary: This is 1st part of java Serialization tutorial. Java Serialization Tutorial: Serialization in java Java Serialization interview questions and answers serialversionuid in java serializationexternalizable in java Transient keyword in java Difference between Serializable and Externalizable in Java Java provides mechanism called […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.