• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

final and transient

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
Can we use final and transient together? And what is the goal if used togather??!
thank you ..
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kedar.. Am able to understand now..
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sun par
u mentioned written out what it means can u explain please
 
sun par
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic