This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static transient variable" Watch "static transient variable" New topic
Author

static transient variable

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Friends,

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.


Javardhan:
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.

thanks
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.

Thanks.
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.


Sanju
marc weber
Sheriff

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
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static transient variable