This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Qn from Marcusgreen test2

 
jaya S K
Greenhorn
Posts: 20
  • 0
  • 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
 
Anupreet Arora
Ranch Hand
Posts: 81
  • 0
  • 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
 
Vicky Nag
Ranch Hand
Posts: 40
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for the explanation.I got the point now.
Regards
jaya
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic