0

I am writing a simple application like bellow:

public class Test1 { String name; public Test1(String name) { this.name=name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return name; } } 

And

public class Test2 { /** * @param args */ public static void main(String[] args) { for(int i=0;i<10;i++) { if(i>6)break; } Test2 test2= new Test2(); Test1 test1= new Test1("Test1"); test2.doTest(test1); System.out.println(test1); } public void doTest(Test1 test1) { test1= new Test1("Test2"); } } 

As I am passing test1 object reference to doTest and reassigning a new value to that object, I believe the output of the avobe program will be Test2. But it gives as Test1 only. It seems the new object created inside doPost is local to that method and does not effect outside. But as Here I am passing the reference to the object, why the value of the object will not be changed to Test2 still?

4 Answers 4

3

It won't change because you're changing the reference inside of the method, not the state of the object.

Method and constructor parameters are always pass by value, and with reference variables that value is the reference itself. If you change the reference assignment inside of the method all it does is re-assigns the reference to the parameter which technically is a local variable, and it will not affect the reference that the original variable pointed to.

If instead the method called test1.setName("Test2"):

public void doTest(Test1 test1) { test1.setName("Test2"); } 

the result would be completely different because now you'd be changing the state of the referred object.

Sign up to request clarification or add additional context in comments.

Comments

2

You can't change the reference to the first object inside the method scope. Meaning the object that is given to doTest will be used as input only.

What you could do is to give the new object back as the result of the method and use the new one outside.

Something like:

public Test1 doTest(Test1 test1) { // do what you need to do with test1 // ... return new Test1("Test2"); } 

and outside you would have something like:

test1 = test2.doTest(test1); // here you are overwriting the reference // with the version returned from the // method invocation 

This approach can be seen in many java APIs, one example would be the java.util.Collections.unmodifiableList method that becomes one list as parameter and gives other as result.

Comments

0

For understanding purpose lets rename argument of doTest() to obj

public void doTest(Test1 obj) { obj = new Test1("Test2"); } 
  • When you call doTest: test2.doTest(test1);you are passing copy of reference(test1)

  • Now obj and test1 both refer to obj with name = 'Test1'

  • In doTest() you create a new object and obj points to it. But test1 still points to object with name = Test1 in main

  • So when call toString() it prints Test1

In short test1 always referred to obj with name = Test1

Comments

0

A method isn't meant to be used like that. To use it you should move the sysout into the method (doTest). And then when you need that instance, call the method with the code it needs inside the method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.