aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing Values" Watch "Passing Values" New topic
Author

Passing Values

Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53




Out put is 20 10 0 20 .

Its about passing values.Could someone help me out with a brief explanation

regarding the output please.

Thanks
sid
Joydeep Nandi
Greenhorn

Joined: Mar 03, 2007
Posts: 3
Hi,
Lets see the program code line by line.

1. class ValHold {
2. public int i = 10;
3. }
4.
5. public class Obparam {
6. public static void main (String args[]) {
7. Obparam o = new Obparam();
8. o.amethod();
9. }
10.
11. public void amethod ( ){
12. int i=0;
13. ValHold v = new ValHold();
14. v.i=345;
15. another(v,i);
16. System.out.println(v.i);
17. }
18.
19. public void another(ValHold v, int i){
20. i=0;
21. v.i=20;
22. System.out.println(v.i);
23. ValHold vh = new ValHold();
24. v = vh;
25. System.out.println(v.i);
26. System.out.println(i);
27. }//End of another
28. }

At line number 15 the ValHold object V is passed inside another() method. As obvious it will pass the referrence of the V object (as V is not a primitive data type). So if we change the state of the V (value of the instance variables of V object) inside another() method, it will reflect the V object. Because inside another() method it is using the same refference of V.

In line 21 we are changing the value of v.i (instance variable of V) to 20. So in line 22 it will print 20.

In line 23 we are creating another new ValHold object vh. By default the instance vairiable i of vh object will be initialized with 10 (ref. line no 2). In line number 24 we are copping the referrence of vh object into the object refference variable v. So after line number 24 v and vh both reference variables are refferencing to the same Object (object created at line number 23).
So in line 25 it will print 10 (The value of i of vh object)

At line 26 it prints the value of i, which is the parameter of the another()method. The value of i is passed from amethod() function (line number 15). The actual value of i in amethod() is 0 (line number 12). So inside another() method at line 26 it will print 0.

After completing another() method, the control will return back to amethod(). And it will print the v.i at line 16. At line 16 it will print 20, the value of v.i, which has been changed inside another() method (Line 21).


SCJP
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
That was awesome explanation .Thanks man
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
When the value (which is the reference to the object in this case) is passed that reference is copied. Initially, it changes the object that's being referenced (changes it's value to 20 - remember that the caller's reference variable is different that the method's 'copy' reference but they point to the same object) However, once the 'copy' of the reference variable points to a different object, it is no longer affecting the original reference in the calling method...so it prints 10 while the calling code finally prints 20. The last poster made a much clearer walk through, but maybe my perspective will add to that too
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53
But how is it that a copy of a reference will be able to point to a different object while the original variable sticks to the previous object acting as a reference ?
Sid Robin
Ranch Hand

Joined: Nov 24, 2007
Posts: 53


Like in the above case sName(original) is pointing to "vandeleur" and sName(copy of sName) is also pointing to "vandeleur wiggy" . My question is how does the compiler distinguish from two sName's ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Values