Transient vs Static variable java

This entry is part 30 of 34 in the series Core Java Course

Here i will show you what is the difference between Static and Transient. In any way these two things are completely different with different scope but people use to ask me this question every time so i am mentioning it here. For previous Serialization articles click here(Pat I) and here(Part II).

Static Variable

Static variables belong to a class and not to any individual instance. The concept of serialization is concerned with the object’s current state. Only data associated with a specific instance of a class is serialized, therefore static member fields  are ignored during serialization.

Transient Variable

While serialization if you don’t want to save state of a variable. You have to mark that variable as Transient. Environment will know that this variable should be ignored and will not save the value of same.

Note*: Even concept is completely different and reason behind not saving is different still people get confused about the existence of both. As in both the case variables value will not get saved.

Difference between these two

Source code : Pay attention to every single code change.

Employee.java

SerializaitonClass.java

DeserializationClass.java

First Execute “SerializaitonClass” and you will get below output.

Second execute “DeserializationClass” and you will get below output

As you can see from output only last name value has been saved. Neither Static nor Transient variables value has been saved.

Now i will change the code a little bit and see what happens.

Employee.java

Again execute the same code and see the output

See the output very carefully. Here value of companyName, companyCEO has been saved but not of address. Also check the saved value of these variable.

Company Name: TATA
Company CEO: Jayshree

In both the case value stored here are taken from class(Employee class) and not from Object(emp object). Also companyCEO variabls value is saved even when it is transient. Because static modifier change the behavior of this variable.

Bullet Point

  1. Static variables value can be stored while serializing if the same is provided while initialization.
  2. If variable is defined as Static and Transient both, than static modifier will govern the behavior of variable and not Transient.

 

Final modifier effect on Serialization

To see the effect of Final modifier i am again changing the code of Employee class.

Employee.java

Again execute the code and see the difference. Output for above code would be

As you can see now address fields is also saved while serialization because it is now Final.

Interface and Final

I have seen a scenario when you can serialize variables inside an Interface which is not serialized.

Employee.java

DeserializationClass.java

Execute above code and see the output.

Here you can see that education’s value is saved. This value is part of an Interface. But as this is constant hence it is saved while serialization.

I think i have covered all possible scenarios. Do let me know in case any particular scenario is not covered in this article. Feel free to say me hi,  if you like this article. And yes i didn’t qualify the interview. :P

Series Navigation<< Java serialization concept and Example Part IIWhat is the use of serialVersionUID >>

38 Comments Transient vs Static variable java

  1. Hieu Tran

    The static variable(education) aren’t saved, because it is a static variable.
    In this case, the line System.out.println(“Education: “+emp.education); printed out the value bevause education’s value is loaded from class, not from serialized object

    Reply
  2. Sarika

    The static variable values are printed because they are accessed along with the class. Static variables can be accessed by classname.staticvariable.
    companyname, companyCEO, education are all static variable values and are printed because they called as classname.variable.

    Reply
  3. Anurag

    Similarly final variables are also not serialized and they are picked from class as soon the class loads itself. You can verify the serialized value in a file and you will not found any final or static variable content there.

    Reply
  4. tamkanat

    hi,
    very nice information.thanks for the effort.
    If u dont mind, can u tell y u dint qualified.
    wat did they asked u in interview that u couldnt answer?

    Reply
    1. Vivekanand Gautam

      Hi Tamkant,

      Thanks for your nice word. I am sorry but i don’t remember the questions. After all these years i can say
      qualifying in an interview is not only dependent on your knowledge but also on Interviewers knowledge.
      I have taken so many interviews my self. And you can not judge someone in 30 min.
      Better we only concentrate on our knowledge and be confident. Someone will understand you.
      And you will qualify the interview someday. :)

      Reply
  5. Naveen Goel

    Hiii
    Do you think there might be some security leak of data while serialization because your data is freely moves into your class and also the outside of the class. So what’s better approach to safe guard data is Inner class concept is good.

    Reply
  6. Naveen Goel

    Hi Vivekananda’s,

    In case of interface data is not serialized because in interfaces by default all fields are final and as you already explained final fields govern behavior of variables as static.

    Reply
  7. Balapoorna

    Hi

    Even if the variable inside the interface is not static or final, will it still retain the value of the variable

    Reply

Leave A Comment

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