Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am starting to learn Java with the Java Trails tutorials offered by Oracle. I am in the section where it talks about passing arguments to methods (http://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html).

It says that primitive types are passed by value, which makes sense to me. Right after that it says:

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

Now this doesn't make sense to me. If an object is passed by value, then changing the fields of that object will change the fields of the copy inside the method, not the original one.

When testing with the program below:

class Point{
        int x;
        int y;

        public static void movePoint(Point p, int x, int y){

class App{
        public static void main(String argv[]){
                Point p1;

                p1 = new Point();
                p1.x = 2;
                p1.y = 3;


                System.out.println("x = " + p1.x +  " y = " + p1.y);

It worked as if the object was being passed by reference (it prints out x = 4 y = 5 after all).

So what exactly did Oracle mean in the passage above? Also, can anyone summarize what's passed by value and what's passed by reference in Java?

share|improve this question
This stackoverflow answer explains it well –  Mike L. Jan 24 '12 at 18:17
@Mile L., I forgot to search on SO before posting here. My bad, and thanks for the link anyway. –  daniels Jan 24 '12 at 18:19
You are right, @daniels: class instances (objects) are passed by reference in the literal sense: through passing the reference (pointer) by value. –  Ingo Feb 26 '13 at 10:37

1 Answer 1

up vote 4 down vote accepted

Java is pass by value. Think of it like a pointer language like C, the value of the pointer (memory address) is being passed, so you have a reference to the same object. Primitives aren't stored internally the same way as Objects, so when you pass a primitive's value, it's the content, not a pointer.

share|improve this answer
Gotcha, so when you pass the name of an object to a method you are actually passing a copy of it's memory address, right? –  daniels Jan 24 '12 at 18:18
Internally, yes. Though, if you were to pass an object (reference), say method(cat), the local variable holding the reference copied from cat could be changed to point to a new object, without changing the actual reference stored in cat. –  Sam DeHaan Jan 24 '12 at 18:21
Right. And I guess that's the reason we say that objects are passed by value (i.e., the value of their addresses). If they were passed by reference in the real sense of the word doing an attribution on the "cat" inside the method would also affect the original one. Correct? –  daniels Jan 24 '12 at 18:25
@daniels: To avoid confusion, it is important to be very precise about terms. Java objects do not have names. Variables have names. A variable can store an object reference or a primitive value. –  kevin cline Jan 24 '12 at 18:42
This specific kind of call-by-value-where-the-value-is-a-pointer is also called call-by-sharing or call-by-object-sharing. IOW: the caller and the callee have different pointers, but they may point to the same (mutable) object. Mutability is what is important here: for immutable objects call-by-value-where-the-value-is-passed-directly and call-by-sharing are actually indistinguishable. –  Jörg W Mittag Jan 25 '12 at 2:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.