Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using final

 
r suraaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.,
my doubt is about final keyword.,

i think a variable whether it is primitive or reference variable ., when it is declared as final variable we can't modify that value.,

but can we assign a final reference variable of an object to another reference variable of the same class?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final in combination with a primitive means that it's value can never change.
Final in combination with a reference means that the reference can never change.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r suraaj wrote:Hi.,
my doubt is about final keyword.,

i think a variable whether it is primitive or reference variable ., when it is declared as final variable we can't modify that value.,

but can we assign a final reference variable of an object to another reference variable of the same class?


It depends.

You can do something like this:



But you can't do this:



Once the reference variable is assinged an object, the reference cannot be changed. Note that the object state can change, such as below:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic