• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object References

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
This is a question from Marcus Green. Why does it print 10,0,20?
I thought it should print 10,0,10.
Isnt the object v that amethod() and another() methods accessing the same??
Thanks and regards,
Kapil
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kapil,
Java passes by reference. That means that you can't change the reference variable but you can change contents of the object that the reference is pointing to. In this case we have:
1. Inside o.amethod().
local variable i set to 99
local variable v created and i member variable set to 30
call another method sending v and i
2. Inside another()
local variable i set to 0
member variable i of passed reference v set to 20
local variable vh created with default member variable i set to 10
local variable v set to point to local variable vh
print out local variable v.i --> 10
print out local variable i --> 0
3. Back from another() call (inside amethod())
print out local variable v.i --> 20
The key is that we have changed the contents (state) of the passed variable v but JVM doesn't allow us to change what v points to! This is a concept that MUST be mastered to be able to program efficiently in Java!
Regards,
Manfred.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic has been discussed here before, perhaps nearly a dozen times. I suggest you use the 'Search' link at the top of this page and search for the string ValHold.
Here are a few links
http://www.javaranch.com/ubb/Forum24/HTML/006262.html
http://www.javaranch.com/ubb/Forum24/HTML/006305.html

Hope that helps!

------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic