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 pass by reference??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "pass by reference???" Watch "pass by reference???" New topic
Author

pass by reference???

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
Hi all,

We say that in Java language we pass by value only, look into the snippet below:


public class PassByRef
{
int i=1;
int j=2;

public void change(PassByRef obj)
{
obj.i=obj.i*10;
obj.j=obj.j*10;
}
public static void main(String a[])
{
PassByRef obj1= new PassByRef();
obj1.change(obj1);
System.out.println("*****obj1.i is **"+obj1.i);
System.out.println("*****obj1.j is **"+obj1.j);
}
}

Here we are passing the reference of the object as arguments to the method when method is called.

Thanks & Regards,

amit
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
I don't see a question here. Are you asking something, or simply making an observation?


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
method change() can alter the state of the object currently referred to by local variable obj1 but change() cannot alter obj1 to refer to a different object. What is being passed by value is not an object but a reference variable.


Mike Gershman
SCJP 1.4, SCWCD in process
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
In other words, the reference is passed by value.


42
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
See if this helps:
http://www.xdweb.net/~dibblego/java/faq/answers.html#q21


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pass by reference???