This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is trasient + static valid ??

 
shubham garg
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does the 'transient' modifier used with 'static' form a legal declaration ?? ...

thanks and regards
shubham
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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 ?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • 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 Java Python
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic