File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes transient ???? 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 ????" Watch "transient ????" New topic

transient ????

Sagar Sharma
Ranch Hand

Joined: Aug 31, 2000
Posts: 92
i have got some doubts on "transient".
Please answer the following questions:
1) Are following variable declarations valid:
(a) transient static int i=0;
(b) transient final int j=0;
(c) transient static final int k=0;
Roberts-Heller-Ernest states on page 90 that "transient variables may not be static or final".
I have compiled the code including the above declarations and there was no error.
2) Can transient variables be instance variables?
I have tried to compile the above declarations as instance variables but reported an error "Interface or class required"
3) Do i need to implement Serializable or Externalizable on the class in which i am declaring transient variables? I have tried to compile with out implementing any of the two interfaces and I do get compiler errors/
Why am i getting these errors???

pls clrify on the above matters.

Jon Aryan

Joined: Oct 06, 2000
Posts: 22
Hi Sagar,
* The class need not be Serializable or Externalizable to declare itz fields as "transient" [ though itz meaningless to do so ]
* But to write the instance of the class ( serialize) into a stream, the class need to implement Serializable or Externalizable, else "NotSerializableException" will be thrown.
This link shown some codes
Ira Jain
Ranch Hand

Joined: Sep 06, 2000
Posts: 70
Regarding your questions,
1) RHE are wrong here . Transient variable can be both static and final. It is also listed in the errata.Please check it from there.
2) For your second question ,please see the following link-
3) About your third one,I don't think you HAVE to implement either of the interfaces . I am not really sure about this so please correct me if I am wrong.

Serge Plourde
Ranch Hand

Joined: Jun 23, 2000
Posts: 140
Hi, Sagar
1)I found nothing in the JLS mentioning the restrictions that you wrote.
And you are right, I compiled it too and it works.
2) The whole point of declaring a member variable transient is to avoid the automatic serialization of instance members that would otherwise happen. It is pointless to declare a member variable as static because static "members" won't get serialized. And it is pointless to declare member variables that are constants (static final).
3) You do need to implement Serializable (or Externalizable) on a class that you will be serializing instances (of the class).
Here is some code, stored in

Hope this helps.
Have a good day.
I agree. Here's the link:
subject: transient ????
It's not a secret anymore!