• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

transient modifier - Q25 of Kumar Majji

 
xing wu
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the question:

The following declaration(as a member variable) is correct or not?.
static final transient int maxElements = 100;
A) It's correct.
B) It's not correct.

My answer is B. As I understand it, transient modifer should not be specified for static variables, as these do not belong to objects.
Thanks for your enlightenment.
 
Ashish Hareet
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ted,
Your reasoning is correct but my jdk 1.2.1 will compile this code . Maybe someone else here will verify this using a later version of the jdk.
HTH
Ashish H.
 
Shishio San
Ranch Hand
Posts: 223
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jdk 1.4.1 compiles it too.
 
Vin Kris
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
transient keyword only refers to the persistence of the variable. It would have been a contradiction with volatile. I'd choose the answer as A.
Static variables need not be persistent. so do final variables.
Any other thoughts on this?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think transient has to do with with serializing (writing) to a stream. All data members are serialized except transient ones. When stream is read back all data members are stored to their last values except transient members are restored to null.
So I guess final or static specifiers are immaterial.
 
xing wu
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I guess final or static specifiers are immaterial.

Yes, I agree. Transient has meaning only in relation to serializing an object's state. For the sake of consistency in it's intent, wouldn't it be a good idea that Java should not allow such syntax? (Just wondering)
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All data members are serialized except transient ones.
Static members are not serialized either as mentioned in http://java.sun.com/docs/books/tutorial/essential/io/providing.html]Providing Object Serialization for Your Classes[/url]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked that 1.4 exam objectives do not explicity include Searilzation. Can we expect that it will not be in exam?
 
xing wu
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All data members of a class that implements Serializable are serialized, except transient and static. Declaring a member variable as both transient and static is redundant. As such, transient modifiers should not be specified for static variables.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic