Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to explain the following question?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the following code :
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
}

the right answer is:
10 0
20
but why not is :
10 0
10
please help me!!
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because in the method another(ValHold, int), even though v was reassigned to a new ValHold, it was only a copy of the orginal reference that was re-assigned the new ValHold.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is what is happening here..
1) With the following call to the method another,
another(v,i);
The object reference v gets copied to method another's v (the formal argument v of method another)
2)Now this v is refering to the object created in the method amethod, so
v.i=20;
changes the value i stored in this object
3)After this, the v in the scope of the method another is made to reference the newly made object referenced by vh,
ValHold vh = new ValHold();
v = vh;
4)And
with System.out.println(v.i+ " "+i); we get 10 0 printed out
method another returns...
5)Now the v in the scope of amethod is still referring to the same object as before
but the value i stored in it has been modified in step 2 which results in the value 20 printed with
System.out.println(v.i);

Correct me if i am wrong.
[This message has been edited by Can Candan (edited June 28, 2001).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Can
I could understand upto step 4 but still unclear about step 5.
You are saying "Now the v in the scope of amethod is still referring to the same object as before". Is it true.
Because now v is referring to vh and value of v.i will be 10.
I have run the program and output is 20 but still I fail to understand it, I think it should be 10.
Can you explain.
Regards
Sandip
 
Can Candan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must understand that the references in the two methods are local and thus seperate.
The line v = vh; makes the local reference variable v of the method another to point to the object pointed by vh, (it is no longer pointing to the object defined in amethod and referenced by the reference variable named v local to amethod after this line)

Are you clear now?

[This message has been edited by Can Candan (edited June 29, 2001).]
 
Beauty is in the eye of the tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic