• 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

Assigning object reference

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.print( v.i );
//output is 20 instead of 10??
}//End of amethod
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();

v = vh;//instead of this statement if it is v.i=vh.i; output is 10010
System.out.print(v.i);
System.out.print(i);
}//End of another
}
could anybody pl explain why the output of this code is 10020 ?
My answer to this question was 10010.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shobha
The object v which is passed to the method 'another' has a initial value for i as 30 and it gets reset to 20 , so the last value of v.i is 200.
The output is 10010 when u have v.i=vh.i becoz when instantiating the ValHold class the initial value of i is set as 10.
If we remove the line v.i=20 then the out put will be 10030.
Hope this is helpful.
Regards
Ravi
 
Ravi Shankar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry about the typo , it is 20 and not 200
 
GM Shobha
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value of v.i is reset to 20.However again v(object) is made to refer to object vh & value of i in vh is 10.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
v.i = vh.i
both are different objects so when u try to assign it is assigning the values and not the reference.
vh.i holds 10, so that value will be assigned to v.i
badri
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi GM

humm...
I remember that this question discuss before.
And I interest to think about why.
When object vh created in the method another(),object v point to the same object of vh,and it is not assign all the value from vh to the v,but it is v point to the same object of vh attribute value.
After end of the method,the object ValHold does not exist.And v use the own object attribute value.
Hope this help

[ May 04, 2003: Message edited by: siu chung man ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic