• Post Reply Bookmark Topic Watch Topic
  • New Topic

final variable and its value  RSS feed

 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any how we can have more than one value in a final variable?
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the final variable is an instance variable, each object can have its own copy. In this case, final means that you can only assign the variable a value one time and then the final variable is immutable.

If the final variable is a local variable, each thread can have its own copy because each thread has its own stack. This case is like the case of a final intance variable in that a value can only be assigned one time and thereafter the variable is also immutable.

If the final variable is also static, there can only be one for the whole class and this is not a variable but is a constant.

Kaydell
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bharat Makwana:
Can any how we can have more than one value in a final variable?

We cannot have more than one value for final variable.

As Kaydell Leavitt has explained , if any parameter of a function is final then during the entire call of the function the value cannot be changed.However you can call the same method multiple times with different values for the final parameter.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Bloch and Gafter's Java puzzler on how final is maybe not quite as final as one might assume.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


but perhaps Dirk is not telling the final truth here.

Bu.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!