• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why are transient static variables allowed?

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just wondering why we can declare a variable to be transient and static. Since static variables are never serialized, it seems that the use of transient is superfluous.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it seems that the use of transient is superfluous


Just like declaring an interface abstract
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Bala wrote:
it seems that the use of transient is superfluous


Just like declaring an interface abstract

Thanks Deepak. I was wondering if there is any scenario which I might have overlooked which made the rationale for this something besides allowable superfluousness.
 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it is superfluous, consider the following situation. [Read the topic wrong!!!]
1) Serialize an Object
2) Do Some operations using the transient variable (increment or pass it to some method..)
3) Deserialize the Object
4) Try accessing the transient variable , it will be initialized to the default value.

But if you have marked the variable static, then it will not be initialized because the static does not belong to a particular instance.

I cranked some code to check this out.



 
Kavita Tipnis
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops sorry , I read your question wrong the other way round!! but never mind the code can be a mock quiz question!
It is superfluous! [Ignore my earlier post's verbage !!]
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem Kavita. You can start your own mock exam product with this example!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic