File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes pass by value or passl by reference! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "pass by value or passl by reference!" Watch "pass by value or passl by reference!" New topic

pass by value or passl by reference!

Manas Ahlaad
Ranch Hand

Joined: Nov 07, 2001
Posts: 165
java says all the variables are passed by value irrespective of they are objects are variables.

Why not objects passed by reference ???

Manas<br />Today If You are not Confused,You are just not thinking Clearly !<br />---------------------------------
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Object references are passed by reference. Yes, a coopy of the reference is passed to a method, but changes made using thet copy of the reference are reflected in the calling method. In the case of primitives, they are passed by copy. i.e changes made in the called method are not reflected in teh calling method.

So much trouble in the world -- Bob Marley
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
It is slightly confusing when you first hear it, but:
All variables are passed by value. For primitives the value passed is simply the value that the variable contains. For reference variables (objects and arrays) the value that the variable holds is a reference to the object or array, so it is a reference that is passed.
hope that helps

Michael Matola
Ranch Hand

Joined: Mar 25, 2001
Posts: 1793
Take a look at the Campfire story on pass by value in Java.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Corey McGlone has a neat Flash animation on his website in the SCJP section that is sure to help any beginner conceptualize what's going on.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: pass by value or passl by reference!
It's not a secret anymore!