• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

transient variables

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can transient variables be final and static?
In a Ques in jwhiz there was an option that transient variables may not be final and static which they considered it to be correct.In explanation they said transient variables can never be final and static.
i have tried the following code and it compiles and runs fine.
transient final static int i=45;
since in the option maybe word we can take this option to be correct.However in khalid book, it is said that transient modifier should not be used on static variables since they donot belong to an object.
kindly comment.
rajashree.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rajshree,
It will not give you any error or exception. But logically there is no benifit to declare a final or static as transient.
Transient is used to protect the persistence(storing the state) of a variable.(Means the variable whose value we don't want to store is declared as transient).When we persist the state of an object, only the value of instance variables are stored.
As static is not come under the category of instance variables,so it will not store its state even if you don't put transient before it.
And if you think logically you do not need to store the value of a final variable as it will never change.So, why to waste memory resources for storing state of those variables which are final.
I hope this will help.
Regards
Gurpreet Sachdeva
For Mock Exams, FAQ and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Gurpreet for ur explanation.
 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic