• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static transient variable

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We can declare a variable

static transient <variable name>;

but its redundant.

correct me if I am wrong.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic