*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes transient modifier - Q25 of Kumar Majji  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "transient modifier - Q25 of Kumar Majji  " Watch "transient modifier - Q25 of Kumar Majji  " New topic
Author

transient modifier - Q25 of Kumar Majji

xing wu
Greenhorn

Joined: May 14, 2002
Posts: 9
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

Joined: Jul 14, 2001
Posts: 375
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

Joined: Aug 29, 2002
Posts: 223
jdk 1.4.1 compiles it too.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: May 14, 2002
Posts: 9
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

Joined: Aug 26, 2001
Posts: 7610
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]


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: May 14, 2002
Posts: 9
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
 
subject: transient modifier - Q25 of Kumar Majji