Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Qn from Marcusgreen test2

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take a look at this program.
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);
}
public void another(ValHold v, int i)
{ i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}}
The output is 10020
Can anyone explain this
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Jaya!

Let me explain it in steps as follows:
1) In the line 9, an new object of ValHold is created. In the next line 10, the local reference variable v is made to point to this new object created. And so the S.O.P at 11 prints the initialisation value of the member '1' which is 10
2) The next SOP at line 12 prints i, which is a local variable in the 'another' method and has been inititialised to 0 at line 7. so the same is printed
3) Taking a look at line 3, we see that a new object of ValHold is created and assigned to a local variable 'v'. It is assigned the value of 30 in the next line. When the 'another' method is called, it has a separate parameter, though of the same name, 'v', but which is local to that method only. This also points to the object whose reference we passed as argument to the method. Now inside the 'another' method, the variable v is referencing the object that was created at 3, and currently has the value 30. At line 8, this value is changed to 20. But the naughty things are done in line no 9 and 10, where a new object is created and the variable 'v' local to that 'another' method is made to point to it. But we must not forget that the variable in 'amethod' still points to the old object, having i=20, which is the value printed at line no 5.
So the net result is "10"+"0"+"20" i.e 10020
I hope things are clearer!
Cheers
Anupreet
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by jaya S K:
Please take a look at this program.
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);
}
public void another(ValHold v, int i)
{ i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.print(v.i);
System.out.print(i);
}}
The output is 10020
Can anyone explain this


Output is 10020 which is right. This is how it works
10 ==>
ValHold vh = new ValHold(); // Initialise i = 10
v = vh;
System.out.print(v.i);
0 ==>
i = 0;// parameter passed to the function "another" which is set to 0
....
System.out.print(i); // changed parameter value printed
20 ==>
System.out.print(v.i); // "another" method changes its value "v.i = 20; "
Hope this helps
V
 
jaya S K
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for the explanation.I got the point now.
Regards
jaya
 
Maybe he went home and went to bed. And took this tiny ad with him:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic