jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes diff between static and transient variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "diff between static and transient variable" Watch "diff between static and transient variable" New topic

diff between static and transient variable

Manish Jaju

Joined: Jul 21, 2004
Posts: 24
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

Joined: Jul 08, 2003
Posts: 24199

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.

[Jess in Action][AskingGoodQuestions]
Manish Jaju

Joined: Jul 21, 2004
Posts: 24
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

Joined: Aug 07, 2003
Posts: 1646
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.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Vijay Vaddem
Ranch Hand

Joined: Feb 13, 2004
Posts: 243
dinosaurs and Santa Claus
I agree. Here's the link: http://aspose.com/file-tools
subject: diff between static and transient variable
It's not a secret anymore!