• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Object I/O with byte streams: transient and static variables aren't written to a file

 
Ranch Hand
Posts: 34
2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading Mala Gupta printed book on OCP, at pag. 488 I read:


When you write objects to a file using ObjectOutputStream, its transient or static variables aren’t written to the file.



Example:


Output is:


RAV4
null:0
2



I don't understand why we get the (correct) carCount (2) value if the rule states that when you write objects to a file using ObjectOutputStream, its transient or static variables aren’t written to the file, whilst we get null and 0 for the transient variables.

Thanks for your help.
 
Sheriff
Posts: 3061
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that static variables belong to the class, not the instance. You've instantiated two Car objects, so the carCount variable is 2. You serialize one of the instances, and while that object stream doesn't include the carCount value, the Car class still retains that value. If you were to print Car.carCount, you'd get the same value. If you were to deserialize into a different Java Virtual Machine, or more specifically, into an environment that used a different class loader, you'd see a different carCount variable. You could do that by streaming the objects over a socket to another running Java program, or by writing the object stream to a file, then running another Java program to read that file.

The transient variables do belong to the instance, but because they are marked transient, they are also not serialized. Therefore unless you give specific instructions on what to do with transients when reading from an object stream, they will be set to default values. For a String (and any other object type) the default is null.
 
Marco Canavese
Ranch Hand
Posts: 34
2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Greg for your quick and most of all very helpful answer.
Got the point!

Cheers
 
Greg Charles
Sheriff
Posts: 3061
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! I'm glad that helped.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic