This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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.