call by value : Java Glossary

go to home page C words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish 2008-04-04 by Roedy Green ©1996-2008 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
call by value
When you call a method, the method sees a copy of any primitives passed to it. Thus any changes it makes to those values have no effect on the caller’s variables. This also applies to references passed as parameters. The caller cannot change the caller’s reference variables, i.e. make them point to different objects, but it can change the fields in the caller’s objects they point to. In Java, you cannot pass objects as parameters, only references to objects.

Java was designed this way on purpose after decades of experience with lower-level languages that let a callee meddle with a caller’s variables. Java code is much easier to understand because it is more encapsulated. You don’t have to concern yourself with unexpected side effects on your variables with every method call.

The following explanation may be helpful to people with experience with low-level languages such as assembler or FORTH, or for people who have poked around learning how Java byte code works. Java evaluates primitive method argument expressions and pushes them to the return stack. Java pushes argument references to Objects as 32 or 64-bit addresses to the return stack. The callee wakes up with the pushed parameters in its stack frame, where it also stores temporary/local/stack variables. Then, when the callee returns, it pops the stack discarding all the arguments. This is why, even if the callee modifies a parameter variable, it has no effect on the caller’s variables. The modification just affects the copy on callee’s stack frame.

However, changes made to Object fields will be see by the caller.


CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.61] The information on this page is for non-military use only.
You are visitor number 22,643. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/callbyvalue.html J:\mindprod\jgloss\callbyvalue.html