File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Badass: Making Users Awesome this week in the Game Development forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Object reference" Watch "Passing Object reference" New topic

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();


public void first()

int i=5;
Value v= new Value();


public static void second(Value v,int i)

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


Output : 15 0 20
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

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:
subject: Passing Object reference