Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface instant variables

 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question for you guys, it from John Meyers' mock test.



The code has compilation error on line 2.

My question is why the compilation pass the line 1? n is a final as well?
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just had another thought, it pass line one because it change the n's object's x value, but does not change reference n to other object (still point to the old object with x changed). Unlike n, number++ change it reference to other primitive object?

Am I right?
[ November 07, 2008: Message edited by: Henry Zhi Lin ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right Henry..
Once a value is assigned to a final wrapper object, its value cannot be changed.
See this link for a related post:
http://www.coderanch.com/t/270629/java-programmer-SCJP/certification/Immutability-Wrapper-Objects
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See you post, and thanks for your explain.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic