This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
shubham garg wrote:does the 'transient' modifier used with 'static' form a legal declaration ?? ...
thanks and regards
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.
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
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
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?
Joined: Jul 20, 2009
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