Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which values will be printed?

 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[/code]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you run ?

P.S: Good Example to pass by value of reference and primitive

 
Purvesh Maheshwari
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ans will be 4) 10020
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
answer is 4. 10020
 
SanjayR Jain
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good question answer will 10020
 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that the answer will be 10020, but can someone explain, why at the and we get 20? Thanks
 
Shams Tabrez Ahmed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code at line 20 changes the value of int i of the object instantiated at line 12
 
Ben Power
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamil Wojcik wrote:We know that the answer will be 10020, but can someone explain, why at the and we get 20? Thanks


At line 20, the another() method changes v.i to 20. Remember, when you pass an Object reference to a method, you pass a copy of the reference, not a separate copy of the object. So now, v.i points to the same v that was declared in line 12. So when the another() method completes and we resume at line 15, the value of v.i has been changed to 20. Does that make sense?

This is an excellent question.

Ben
 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey when you pass a referance variable to a method you are not only passing the address of that referance variable but that is copy of that referance variable address.
This means that you are passing copy of referance variable to ValHold v.
Then you are changing that value to vh(another object) The previous referace object is still same uneffected.

Remember you can always change the value of the passing object but you can't change the passing object to refer to other object.

 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey when you pass a referance variable to a method you are not only passing the address of that referance variable but that is copy of that referance variable address.
This means that you are passing copy of referance variable to ValHold v.
Then you are changing that value to vh(another object) The previous referace object is still same uneffected.

Remember you can always change the value of the passing object but you can't change the passing object to refer to other object.

Answer is abc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic