0

If I pass a object into a method and I change that object in that method will it keep its changes? I'm not sure if I've worded this right so I'll give an example:

public void methodThatChangesX(ClassWithVariableX var) {
    var.x = 5;
}

ExampleClass var = new ExampleClass(); 
//ExampleClass's x's default is 6

methodThatChangesX(var);

System.out.println("x = " + var.x);

Would var's x be 5 or 6? Is there a reason for this?

Teeks Noj
  • 46
  • 4

2 Answers2

4

The value would be 5, but this behaviour applies only to class types.
If you pass an argument of a primitive type (such as an int), you will be dealing with a copy inside the method.

In fact, even the class-type argument that is passed in is really only a new reference to the same object, so if instead of var.x = something you said var = something, that would not end up changing the caller's var.

ETA: Some in-depth explanations of what is really going on there:

Community
  • 1
  • 1
hemflit
  • 2,819
  • 3
  • 22
  • 17
  • 2
    How can you say that the value will be 6? You said it your self that "the class-type argument that is passed in is really only a new reference to the same object" in which case the value of `var.x` **will** change as long as you pass `var` it self so the answer is 5. – Dima Maligin Feb 26 '15 at 23:03
  • The value of `var.x` will be 5. Downvoting as it is a wrong answer. – fps Feb 26 '15 at 23:08
  • I had misread the question as 5 being the "default" and 6 being the new value, while it had actually been asked the other way around. Sorry about that - edited now to correct the digit. The explanation given in the answer is however correct. – hemflit Feb 26 '15 at 23:34
  • @hemflit I didnt downvote, but that 6 there really triggered a swift response since the answer was really misleading. +1 for the edit – Dima Maligin Feb 26 '15 at 23:42
  • Thank you @DimaMaligin, the answer was definitely misleading that way, and your reaction was 100% justified. – hemflit Feb 26 '15 at 23:53
2

The value will be 5. Although all parameters are passed by value, in the case of <? extends Object> the value of ? is an address that references the Object, so whats is happening when you pass an Object is that its address is passes by value. Which means if you use that Object its address is the same as the one that got passed, so while you cant assign a new Object to it you can pretty much do what ever you want with its attributes and this will change the original Object's state.

consider this class example:

public class Test {
    public int i;

    public Test(int i) {
        this.i = i;
    }
}

and this static method:

public static void changeAtrib(Test t) {
    t.i = 0;
    t = new Test(7);
}

now lest create an instance of Test and pass it to our method:

Test test = new Test(5);
changeAtrib(test);

When the method runs the variable t inside holds the address value of the original test.

The first line in the method is: t.i = 0;

Which is referencing i using test's original address, that means that i will change in the original test as well.

Now lets take a look at the second line: t = new Test(7);

Now remember t holds the address value of test and t = new Test(7); only creates a new Test object and assigns its address to t, that operation has no impact on test since it only changes the value t hold and makes t reference a new object.

Dima Maligin
  • 1,386
  • 2
  • 15
  • 30