File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why is the output ........ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why is the output ........" Watch "why is the output ........" New topic
Author

why is the output ........

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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);//ABC
}//End of another
}

Why is the output at ABC for v.i as 10 not 20 ?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,

in another method you create new valhold obj
and assign its ref to v.
since the newly created obj has the value of i=10 the and the same object reference is assigned v
therefore v.i=10;

but in main u'll get the valuf of v.i=20.

try to evaluate the problem step by step

Hope this helps

Sandy
[ September 18, 2005: Message edited by: Sandeep Chhabra ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
so you mean that as the new instance of class is created
[ValHold vh = new ValHold();]

so it holds the instance variables i=10; RIGHT???/

if the new instance of class is not created
then the value of v.i=20 is printed right???
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
You have got the point now...

Sandy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why is the output ........
 
Similar Threads
question about passing parameters by value or by reference
urgent pls explain
Tricky question
urgent pls explain
question about passing parameters by value or by reference