• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff between static and transient variable

 
Manish Jaju
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to the the difference between static and transient variable used in Java Programming. and how these variables make difference in case of serialization.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, static variables are variables that belong to a whole class rather than to a single instance of that class. "Transient" is actually just a notation to be used by the serialization machinery; it means "don't serialize this member."

Neither static nor transient variables are serialized when you serialize an object, but for completely different reasons: statics because they're not part of any object, and transients because, well, because that's what transient means.
 
Manish Jaju
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thax for the response.
But still I am not clear with the difference/s....
As per the above explaination, there should not be any difference between static variable and transient variable while serializing process.... Then why to use transient, we can evidently opt for static only???
Please clarify.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static members are not serialized because only non-transient instance members are serialized when serializing an instance of a class.

Transient instance members are not serialized for the same reason, but that doesn't mean there's no difference between static and transient instance members.

First, a member is either a static or instance member; they are mutually exclusive. Transient is a modifier that can be applied to instance members only and is stricly related to serialization.

Static members, as Ernest said, belong to the class as a whole -- there is one per class -- while instance members belong to instances of the class -- there is one per instance of the class.

Static members are similar to global variables in C, but there are subtle differences due to the complex nature of class-loading.

Try the following metaphor on for size. Dinosaurs don't exist because they became extinct millions of years ago. Santa Claus doesn't exist because he is a myth. Neither exist, but that doesn't mean there is no difference between dinosaurs and Santa Claus.

Hmm, I think I just committed a faux pas by assuming you'd know the myth of Santa Claus. He's a mythological man who brings presents to good kids on Christmas (December 25th). He did not roam the earth 65 million years ago as dinosaurs did, nor is he the ancestor of modern-day birds as dinosaurs are theorized to be.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dinosaurs and Santa Claus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic