• Post Reply Bookmark Topic Watch Topic
  • New Topic

Objects declared as final their reference cannot be changed.  RSS feed

 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Code ranch,

Objects declared as final can always be initialized only once. If there is an attempt to change the object reference or the contents, then compilation error will occur.

According to me the above statement is true.

But i have read in an documentation it is false.

Can you please help me out in this as if we change the object reference which is declared as final will result in compilation error.


Regards,
D.Srikanth
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking to the code snippet above, and the statement you posted, try to see what this code proves wrong about the statement you posted?
 
srikanth darbha
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Looking to the code snippet above, and the statement you posted, try to see what this code proves wrong about the statement you posted?


I can see that modifying content will not result in any error but reference change will give an error.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
srikanth darbha wrote:I can see that modifying content will not result in any error but reference change will give an error.

Here you go. So what the correct answer you can conclude about the statement:

"Objects declared as final can always be initialized only once. If there is an attempt to change the object reference or the contents, then compilation error will occur."
  • true
  • false


  • ?
     
    Campbell Ritchie
    Marshal
    Posts: 56527
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is because the final modifier is applied to the reference, not the state of the object. You can't writeWhat people misunderstand is the difference between changing the identity of what the reference points to (final does that) and preventing changing the state of any object pointed to. You can always change the state of a mutable reference type. There are various ways to mitigate that latter problem, e.g. by taking defensive copies (see Joshua Bloch Effective Java™ 2/e pags 184‑188) or reducing mutability (Bloch pages 73‑80 and Java™ Tutorials). You will notice that the examples of immutability use final inside the class; you cannot “inject” final‑ity into a class by any sort of technique like the code I showed.
     
    srikanth darbha
    Ranch Hand
    Posts: 56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Liutauras Vilda wrote:
    srikanth darbha wrote:I can see that modifying content will not result in any error but reference change will give an error.

    Here you go. So what the correct answer you can conclude about the statement:

    "Objects declared as final can always be initialized only once. If there is an attempt to change the object reference or the contents, then compilation error will occur."
  • true
  • false


  • ?


    The answer is true as i have said earlier..
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16057
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Read the statement again, carefully:

    Objects declared as final can always be initialized only once. If there is an attempt to change the object reference OR THE CONTENTS, then compilation error will occur.

    What happens if you change the contents of the object - do you get a compilation error or not? Note that the statement claims that you would get a compilation error.

    This is what you said yourself:
    srikanth darbha wrote:I can see that modifying content will not result in any error but reference change will give an error.


    So, do you still think the statement is true?
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!