wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Qn from Marcusgreen test2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Qn from Marcusgreen test2" Watch "Qn from Marcusgreen test2" New topic
Author

Qn from Marcusgreen test2

jaya S K
Greenhorn

Joined: Aug 18, 2003
Posts: 20
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
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
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
Vicky Nag
Ranch Hand

Joined: Jul 02, 2003
Posts: 40
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

Joined: Aug 18, 2003
Posts: 20
Hi
Thanks for the explanation.I got the point now.
Regards
jaya
 
GeeCON Prague 2014
 
subject: Qn from Marcusgreen test2