This question is from Poddler's mock exam ( I got this from maha anna's mock exam list). This is how it goes, Question : The following declaration (as a member variable) is legal static final transient int maxElements = 100; A. True B. False
The answer given is A. That is 'True'. But I think it should be false as 'Transient variable may not be final or static' (page 90 of RHE). [This message has been edited by Sandra Marti (edited July 19, 2000).]
1.static: A transient variable can't be static as only variables attached to objects r relevent to be made transient. Its no use to make a class variable transient as class variable can't be serialized/de-serialized. 2.final: I also read that final vraibles can't be static, can't relate tho. May be that while object is being de-serialized, its not possible for JVM to assign value to the final variable?? right?
Guys, Have you tried compiling this line of code. There is absolutely nothing wrong with it and it is a perfectly legal declaration. Static means the variable is bound to the Class object instead of an object instance. However, it can still be accessed via the object instance and can be treated as an instance variable. Being static has nothing to do with being final or being transient. Similarly, final means once initialized, it cannot be changed. It doesn't mean it cannot be or should not be serialized!. Again, final, static and transient are keywords which serve different intents and are not mutually exclusive. Hope this helps Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Joined: Jun 08, 2000
Thanks Ajit and Hemanshow I found that this was already discussed in an earlier thread and apparently the statement in RHE is wrong. I better refer to the errata.