• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Transient & static members (Selikoff, Boyarsky)

 
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a question regarding static & transient members:

Considering that "transient variables and static class members are ignored during the serialization and deserialization process" (page 427):
1) Why does my code output the static char type ('C' in this case)?
2) When I read my transient String, it still outputs null, why? "it is important to check for null values when reading from a serialized data stream. We rely on the instanceof operator to always return false for null values". (page 431)

 
Helene Shaikh
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, when I use a constructor without arguments, my code still outputs age 3 and type 'c'. But the explanation on page 432 states that "when the object is deserialized, no class constructor or default initializations are used"

 
Ranch Hand
Posts: 99
15
Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Your static variable type is common to all instances of Animal. It is initialized the first time the class loads. After you create the first object, the type variable is assigned 'C'. Your class Animal is still there, so the class variable type is still there. It is not deserialized, but common to all instances of Animal and still has the value 'C'.

2) A transient instance variable is not deserialized. You check only if the deserialized object is instanceof Animal (not null). This line has nothing to do with the variable name.

3) The variable age is deserialized. No constructor is called. This information was hold in the file cat.html
 
Sheriff
Posts: 9689
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The test for static you can easily do by adding a line to your main method which sets the value to something else and see if it is deserialized.


As far as the null check is concerned, you already have that check in your code at this line
 
reply
    Bookmark Topic Watch Topic
  • New Topic