Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is transient allowed with static?

 
Louie van Bommel
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Can a variable be transient if it is static?
2. If so, why? As far as I know java.io Object writing routines only write out instance variables, and not class variables. Aren't objects serialized without their static variables?
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting question.

Class variables (static) can be declared as transient, however class variables are not serialized and de-serialized. Only the persistence state of the object will be serialized, class variables will be re-created or intialized when the object is constructed after the de-serialisation process.

I recommend using the keyword transient for class variables, this makes it very clear to the programmer of the class that this variable will not be serialized.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you IO the class itself?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic