• 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

is trasient + static valid ??

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does the 'transient' modifier used with 'static' form a legal declaration ?? ...

thanks and regards
shubham
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shubham garg wrote:does the 'transient' modifier used with 'static' form a legal declaration ?? ...

thanks and regards
shubham


As far as i understand, statics are implicitly transient, as their state is not a part of state of an object, but a part of state of the class itself. So static vars don't serialize with instance variables. And since code with "transient static int myVar=0;" compiles well, yes, it is legal, but behaves the same way as just "static int myVar=0;" Correct me if i am wrong.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shubham garg wrote:does the 'transient' modifier used with 'static' form a legal declaration ?? ...

thanks and regards
shubham



Did you try ? What happened when you did ?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually this is really tricky..... As statics are not at all valid for serialization as serialization is for objects an statics are not object variables...... but transient variables are object variables but we make it transient because we don't want eh value to be recorded or saved and also we make it transient for a object reference if that class is not implementing serializable.

Well i think transient variables are instance variables which are not used for serialization.

The java compiler does not complaint if you declare a static member field as transient. However, there is no point in declaring a static member field as transient, since transient means: "do not serialize", and static fields would not be serialized anyway.

Its like static variables are transient but transient variables cannot always be static. its more like the equals and hashcode thing
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well thing with static and transient is ok..... What will happen if the variable is transient final and more over if the variable is transient static final?
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitish Bangera wrote:well thing with static and transient is ok..... What will happen if the variable is transient final and more over if the variable is transient static final?


What transient does - it says compiler that this particular variable doesn't go to serialized form. So for this mechanizm no matter if the variable final or not - you either want to serialize you final object variable or you don't want this variable to be a part of serialized state of your object.
So is absolutely legal.

And if the variable is static - it just is not serializable transient or not (statics kinda implicitly trancient). So is also legal, but you can omit the keyword "trancient" with the same result
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic