Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

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).]
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic