• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using final

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
reply
    Bookmark Topic Watch Topic
  • New Topic