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 confusion in 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 "confusion in transient" Watch "confusion in transient" New topic

confusion in transient

Indika Sampath

Joined: Jun 20, 2007
Posts: 2

This statement is complile successfully. But in Kathy & Bates book, they have clearly mentioned that transient can be applied only to instance variables.

can anybody explain this ?

[ June 20, 2007: Message edited by: Barry Gaunt ]
Riya Pant

Joined: Feb 17, 2006
Posts: 28
i also want to know the answer
What I understand from Serialization in context of static and transiet is:
If a class has static variables and this class's object goes under serialization,then this static variable do not get re-initialized when deserialized. But if a variable is declared Transient, it will be re-initialized to its default value when the object is deserialized.

But in the scenario, mentioned by indika, how this variable will behave when the object will be deserialized
Prafulla N. Patil
Ranch Hand

Joined: Mar 20, 2006
Posts: 106
this will not give error

Thanks, Prafulla Patil
Riya Pant

Joined: Feb 17, 2006
Posts: 28
But what will be its behavior..Will it work like transient or like static variable ??
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Perhaps going through thread will help you to understand a bit more.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Yes, They are right.

"Transient" is applicable for only instance variables and NOT for static variables.

Because, the static variables belong to class and NOT to objects, thereby serializing it will not really make sense since the concept of Serialization is storing the state of object and NOT the class!

[ June 21, 2007: Message edited by: Raghavan Muthu ]
Bharat Makwana
Ranch Hand

Joined: May 21, 2007
Posts: 107
Originally posted by Indika Sampath:

Well,If you write above statement in your code,compiler will treat it as static variable.I have run code with "final static" and transient.

ॐ सर्वे जना: सुखिनो भवन्तु , तथास्तु |
'May the whole world be happy, so be it'

I agree. Here's the link:
subject: confusion in transient
It's not a secret anymore!