Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static transient variable

 
Ja vardhan
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vipul patel
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can declare a variable

static transient <variable name>;

but its redundant.

correct me if I am wrong.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic