1

I've read a gazillion times that in Java, arguments passed to methods cannot be modified by the methods. Nonetheless, I find I can modify objects I create as opposed to Java Objects. Consider the following code:

    // My Integer Object
    class MyInteger {
        Integer val;
    }

    // Change Java Object
    public void test1() {
        Integer intval;      // Integer object
        intval = 123;
        setInteger( intval );       // THIS DOESN'T WORK

        TTY.putR( "Integer Object="+intval);
    }

    // Change My Object
    public void test2() {
        MyInteger myInt;    // MyInteger object
        myInt = new MyInteger();
        myInt.val = 456;
        setMyInteger( myInt );      // THIS WORKS!

        TTY.putR( "MyIntegerObject=" + myInt.val );
    }

    // Set an Integer object
    public void setInteger( Integer val) {
        val = 888;
    }

    // Set a MyInteger object
    public void setMyInteger( MyInteger myint) {
        myint.val = 999;
    }

test1 doesn't work as I have been warned. But test2 works just fine. AFAIK, both are objects and are passed by reference. So how come one works and the other doesn't? (Note: TTY is my print function)

DontPanic
  • 2,164
  • 5
  • 29
  • 56

5 Answers5

8

You have either read things that were wrong, or misunderstood what you've been told.

If you pass 'a' to a java method, you cannot have the method change 'a' to be something other than 'a'. However, you can modify 'a'.

In other words, you cannot create an object of the same class as 'a' and return that in place of 'a'. The only way you can return an object created by the method is either to have a place to put a reference to that new object within an object passed to the method, or to return it as the return value from the method.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • This explains what I am seeing best. I thought objects were always passed by reference, but I guess what your saying is the the object passed is a COPY of the original, so the method is just modifying the copy. Is that correct? – DontPanic May 21 '18 at 20:49
  • 1
    No, exactly the opposite. The object IS passed by reference, that is, a pointer to the object is passed to the method. That pointer cannot be changed by the method. But the method can use the pointer to change things within the object, and those changes are visible to the caller, and to anyone that has that reference. When your program executes `MyClass o = new MyClass();`, the variable `o` now contains a reference to an obejct instance of MyClass; we speak of `o` as the object, but it is really an object reference. When we pass "`o`" to a method, we are passing the reference. – arcy May 22 '18 at 10:20
3

The best way I've seen this explained:

You pass an object A pointing to a memory address P.

A ===> P

When you modify A by doing A.foo = bar, A is still pointing to P, so the object at P has its property foo changed. However, let's say you want to completely reassign A, and so do A = new MyCoolObject(). This means

P_New <=== A ==/=> P

So when you modify A by doing A.foo = bar, A is no longer pointing to P, so the object at P_New has its property foo changed, but the object at P remains unchanged. This means when you exit the method and go back to whatever parent called the method, A will be completely unchanged.

Disclaimer: I saw this on another Stack Overflow article probably 5 years ago, and am too lazy to find it. If you're reading this right now and you're the person who wrote this, thanks, and forgive my casual plagiarism.

Grey Haven
  • 380
  • 2
  • 13
0

Because MyInteger the val is a public variable. So ANYONE can modify it.

 // Change MyInteger object
    public void setMyInteger( MyInteger val) {
        val.val = 999;  // ACCESING TO A PUBLIC VAR and SETTING IT
    }

// Change an Integer object
public void setInteger( Integer val) {
    val = 888; // Accesing to the value of the Variable and not modifing it
}
Gatusko
  • 2,503
  • 1
  • 17
  • 25
0

I think you are confused by pass-by-reference vs. pass-by-value. Read this to help clear it up.

You might also have misinterpreted what you've read. A mutable Object can be mutated anywhere -- where it is created or by a method it is passed to.

Good luck.

Jamie Bisotti
  • 2,605
  • 19
  • 23
-1

Java is a pass by value language. When you invoke any method with argument it creates new variable and you are changing this variable.

You can look at this also, Is Java "pass-by-reference" or "pass-by-value"?

Hatice
  • 874
  • 1
  • 8
  • 17