Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why are transient static variables allowed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why are transient static variables allowed?" Watch "Why are transient static variables allowed?" New topic
Author

Why are transient static variables allowed?

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

it seems that the use of transient is superfluous


Just like declaring an interface abstract


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Sep 21, 2008
Posts: 177
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

Joined: Sep 21, 2008
Posts: 177
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

Joined: Dec 16, 2008
Posts: 1032
No problem Kavita. You can start your own mock exam product with this example!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why are transient static variables allowed?