*
The moose likes Beginning Java and the fly likes Passing Object reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Object reference" Watch "Passing Object reference" New topic
Author

Passing Object reference

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi all,


Can anybody please expalin me the following program as how I am getting the output.The progem is running correctly but I need an explanation as I am cofused with passing an object reference. How v.i=15?

class Value
{
public int i=15;

}

public class Test
{

public static void main(String args[])

{

Test t = new Test();
t.first();

}

public void first()

{
int i=5;
Value v= new Value();
v.i=25;
second(v,i);
System.out.print(v.i);

}

public static void second(Value v,int i)
{

i=o;
v.i=20;
Value val = new Value();
v=val;
System.out.print(v.i + " "+ i);

}
}


Output : 15 0 20
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

On line 37 (in the method second), v.i = 15 because v is a reference to the object created on line 35, which has an i value of 15.

On line 26 (in the method first), v.i = 20 because v is a reference to the object created on line 23

Note that the two vs are different -- they are references with the same name that reference the same object, but they are different references. You would get the same results (an things mightl be a little clearer) if the method second were written as:



Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Object reference
 
Similar Threads
java about pass by value,why?
code not understood
Reference passing
please explain
Can you explain this?