• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Member Variable.

 
Roll
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want a member variable of a class to be changed in any way then what would you do?
1) Make it private
2) Make it public
3) Make it final
4) Make it private and provide an accessor method that returns its value.
I think the answer could not be final because
Even you have declared member variable as final and you haven't intialized it you can intianlize it in constructor.
So, then answer cannot be final?
Can anybody else think what could be the posiible answer ?
 
paul wheaton
Trailboss
Pie
Posts: 21600
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to have an unitialized value to be final and you cannot set to anything in the constructor? What would be the use of that?
I think "final" is the right answer. At the very least, this is its purpose.
 
Luke Simon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is final, because final variables must be initialized once and only once. A final variable is a constant. This means that it only has one value in its entire lifetime. Before initialization, it is not considered to have a value.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roll .. welcome to JavaRanch
We have a Names Policy. Please read the document and re-register under a name that complies with the rules.
Thanks for your co-operation.
Jane
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic