• Post Reply Bookmark Topic Watch Topic
  • New Topic

obj post-increment  RSS feed

 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone

I am busy with a mock question from Enthuware and need some assistance to better understand why the answer is 6 10.



I understand why val 2 = 10 at the end, but if all the obj increments is post why did it eventually increase after the method call.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will,
In the method, any changers are local to that method. So obj changes to point to a different object reference inside the method but not in the main(). This means you can pretend that whole method isn't there!

Which means the code might as well say:
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so then val1++ and ++val2 creates new objects with the incremented value which is then printed.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And stored in the original reference. Right.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meaning the original reference is then pointed to the new incremented val1 and val2.

Awesome.. thank you. damn these OCA mock exam questions are tricky. Is the actual OCA exam questions as difficult.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Dev wrote:Awesome.. thank you. damn these OCA mock exam questions are tricky. Is the actual OCA exam questions as difficult.

A little easier. But that's why the practice questions are tricky. So you learn to look for this stuff.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!