• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding refernce variable passing to method

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Please have a look at the following code:



The output is 100 100 1 1 1. When we pass the reference variable h1, the copy of bits that refer to the object referred to by h1 on heap are passed to the method doStuff(). Correct? Then why is it printing 100 again for h1 roomNr? It should print 1 .
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the following code return 100 100 1 1 1 explains it.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when we say inside the method doStuff, we are creating a new reference hotel and the original h1 reference remains unchanged. Correct?
 
Zhenyi Luo
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:So when we say inside the method doStuff, we are creating a new reference hotel and the original h1 reference remains unchanged. Correct?


You are right.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic