Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static transient variable 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 "static transient variable" Watch "static transient variable" New topic

static transient variable

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169

Somewhere I read like- any how 'static' fields are not serialized, there is no special meaning of using 'transient' like this:

static transient int st = 101;

Please tell me can we use static along with transient or not and what will happen if we use like this?

what is the relation between static and transient?

Thanks in adavance.

vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
Je verdan,

static and transient have nothing to do with each other. static means your variable is accessible at class level without instantation.

whereas transient has meaning related to multiple threads using veriable. so to synchronize, use transient.

so no harm. go for it.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Transient modifier is not related to threads (only volatile is). It is about serialization. Transient means that the field is not automatically serialized.

Static fields are always transient. They can't be serialized because they don't belog to any instance.

I am not sure if it is permitted to write

But transient has no meaning it this case, it is equivallent to
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
I agree Vlado. Sorry for my mistake. what i wrote about transient is actually applicable for volatile. Ya, you are right, transient variables are not saved when object is serilized to file or on network stream. static are not part of any object so they are by default transient. But I tried transient static syntax and it works.

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Vlado/ Dilip,

Thanks for yuoe reply. With your words, I understood the concept why static fields are not serialized. With this concept, I think its not useful to declare staic field as transient.

I tried this on my IDE Eclipse-
static transient int t = 101;
IDE is not showing any error.

manogna edintipal
Ranch Hand

Joined: Aug 16, 2005
Posts: 51
We can declare a variable

static transient <variable name>;

but its redundant.

correct me if I am wrong.

marc weber

Joined: Aug 31, 2004
Posts: 11343

That's correct: static variables can be labeled as transient, but are not stored to an object's persistent state anyway, so the transient modifier has no effect.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: static transient variable
jQuery in Action, 3rd edition