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

final and transient

zaghal mohd
Ranch Hand

Joined: Jun 27, 2002
Posts: 43
hi all
Can we use final and transient together? And what is the goal if used togather??!
thank you ..


SCJP,SCWCD,CCNA,CCAI,MCP,MCSA,MCSE<br />working on CCNP+SCBCD
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
final: means a value has been finalized at compile time and it can not change at run time.
transient: means if object is saved to disk, the transient value will not be saved.
I guess it makes sense to use them together because you do not want to save the values you know are final and can be read back by compiler at a later time.
[ October 10, 2002: Message edited by: Barkat Mardhani ]
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Hai,
I used something like
final transient int jh=8;
in my program and it gave me the error..
----------------------------------------
modifier transient not allowed here
final transient int jh;
^
1 error
-----------------------------------------
so how can the final and transient be used together
Thanks


Sunita<br />SCJP 1.4
Kedar Joshi
Greenhorn

Joined: Aug 26, 2002
Posts: 12
hi Sunita,
you said is true ..but partially!
put the same line of code as class member, it will work!
reason is transient/volatile can be applied only to member variables.
further following combinations are allowed for member variables but not for local
final transient
transient volatile
"final volatile" can never appear together
I hope this will help you
Kedar


Sing, laugh, dance, and meditate
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Thanks Kedar.. Am able to understand now..
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

final: means a value has been finalized at compile time and it can not change at run time.

Not exactly right. A final variable once initialized is not allowed to be assigned again.
final double variable = Math.random();
The value assigned to variable is not computed at compile time.


SCJP2. Please Indent your code using UBB Code
sun moon
Greenhorn

Joined: Oct 09, 2002
Posts: 28
hi,
can anyone explain me the use of
final transient int k =100;
as a member viriable.
What i infer is
If any variable is assigned with a keyword fianl then the value cannot be changed.
But i think with tranisent it is not the case.
Then actualy where it is used can anyone exaplain me with example. Please
Thanks in advance
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
transient means that variable is not written out when the object is serialized, think when it is made final that value cannot be changed and is not written out during serialization...
sun moon
Greenhorn

Joined: Oct 09, 2002
Posts: 28
hi sun par
u mentioned written out what it means can u explain please
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
for example if you write an object and it has a transient variable when you write out the object to a file for example the transient variable will not be written to the file...
zaghal mohd
Ranch Hand

Joined: Jun 27, 2002
Posts: 43
Hi all
transient means that variable is not written out when the object is serialized, think when it is made final that value cannot be changed and is not written out during serialization...
I think better explanations as sun said
But correct me if I am wrong
transient variables cannot be serialized.
and final cannot be changed. overridden.inherited.
and variable should be final or a constant

My question Any goal for using both together
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
You can have final and transient together for an instance variable (transient NEVER makes sense with a static variable, because static variable values are never saved as part of an object's state).
You need to be allowed to mark a variable as transient for whatever normal reasons you have (it's not serializable, or its really big, or its a security risk to let this part of the object be saved into a file, etc.).
BUT... its dangerous if the variable is final.
Remember that final means that once assigned the value can't be changed.
And you get TWO chances (as of Java 2) to assign that initial (and forever) value:
1) Explicit initialization (in other words, at the moment of declaration -- final int x = 17;
2) In the constructor. If you haven't assigned the final variable an explicit value, then the compiler will make sure that you have given it one in the constructor. This is called a BLANK FINAL (a final that is declared but not explicitly initialized.)
With transient, the problem is that transient variables are restored at de-serialization with their default values, because explicit initialization doesn't run and neither does the constructor when an object is de-serialized. So an int variable explicitly (or in the constructor) initialized to, say, 5 will come back as 0.
BUT... with a transient variable that is also FINAL, Java does a little trick and runs the explicit initialization again -- just for the final variable, not the non-final transient variables -- so that the final variable is assigned its original value again.
BUT... that only works if the final transient variable is NOT a BLANK FINAL.
So the bottom-line: you can use final and transient together, and may need to. But if you DO use a final variable, you better give it an explicit value rather than wait for the constructor, or you can have a scenario where the object gets de-serialized and the final variable now has a different value (the default values) and you can't reassign it!!
(and even the normal way to recover from this -- using a private readObject method -- won't work because you can never, ever reassign a value to a private variable --- even while its in the process of being deserialized. Only Java itself can re-run your explicit initialization for that final transient variable).
cheers,
Kathy


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final and transient
 
Similar Threads
transient modifier
how to exclude a class member from hibernate mapping?
is trasient + static valid ??
transient variables
making a mostly anonymous Comparator be Serializable