This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding refernce variable passing to method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding refernce variable passing to method" Watch "Doubt regarding refernce variable passing to method" New topic
Author

Doubt regarding refernce variable passing to method

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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 .


~ Mansukh
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1167
Why does the following code return 100 100 1 1 1 explains it.


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Sep 03, 2012
Posts: 33
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.
 
wood burning stoves
 
subject: Doubt regarding refernce variable passing to method
 
Similar Threads
confuse for static method
help sought in Collections ?
hashtable working & overriding equals
Please explain the output
Static Doubt