• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is happening to my transient variable  RSS feed

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys ! l know that transient variable does not get serialized, now I want to know what is happening to my transient variable in the following program! My objects s1,s2 and s3 is serialized and saved to a file. Next I retrieve it and save in the collections. And when I display its transient variable...it is 0. How? here is the code

output

Here are my questions...
what is happening to trans when I save the objects s1,s2, s3 to the file and again when I am retrieving the object from file.
The value I gave to trans is 20. Why is displayed 0?
 
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you mark an instance variable as transient and serialize the object, after deserialization, that particular member will get its default value, which in your case is an int, so it will be initialized back to 0. That is how it works.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
midhuna peru wrote:And when I display its transient variable...it is 0. How?


What did you expect it to be?

What did you think "transient" would do?
 
midhuna peru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since I initialised trans with 20 I expected it to be that. Now is my file giving me an object without trans here in line 49

Object obj=ois.readObject();

and then what is happening during these lines 50 and 51

if(obj instanceof Student)
studentList.add((Student)obj);
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
midhuna peru wrote:Since I initialised trans with 20 I expected it to be that.


Ah, I see. You thought the initializer would be run when the object was deserialized. That was a reasonable guess, and I don't know if I'd even have known the answer off the top of my head. It appears it doesn't though.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn’t find anything about serialisation in the Java Language Specification, nor did I see anything about initialisers in the serialisation specification.
 
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default constructor doesn't run during deserialization, which lead to default value for this data-type.
 
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I couldn’t find anything about serialisation in the Java Language Specification, nor did I see anything about initialisers in the serialisation specification.

It's under 3.1 The ObjectInputStream class, in item 11:
11. An instance of the class is allocated. The instance and its handle are added to the set of known objects. The contents restored appropriately:

a. For serializable objects, the no-arg constructor for the first non-serializable supertype is run. For serializable classes, the fields are initialized to the default value appropriate for its type. Then the fields of each class are restored by calling class-specific readObject methods, or if these are not defined, by calling the defaultReadObject method. Note that field initializers and constructors are not executed for serializable classes during deserialization. In the normal case, the version of the class that wrote the stream will be the same as the class reading the stream. In this case, all of the supertypes of the object in the stream will match the supertypes in the currently-loaded class. If the version of the class that wrote the stream had different supertypes than the loaded class, the ObjectInputStream must be more careful about restoring or initializing the state of the differing classes. It must step through the classes, matching the available data in the stream with the classes of the object being restored. Data for classes that occur in the stream, but do not occur in the object, is discarded. For classes that occur in the object, but not in the stream, the class fields are set to default values by default serialization.

(Emphasis added by me.)
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That’s it. Well done finding it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!