0

I'm confused about ArrayList with another variable that can change the value from another class. I have two classes and two examples with the ArrayList and Object variables. Can somebody help me to understand why it's different and if there is a solution to make it have the same effect?

This is the code with ArrayList:

import java.util.List;

public class Test {
    private String name = "Fifi";
    
    void fill(List<Test> a) {
        a.add(this);
    }
}
import java.util.ArrayList;
import java.util.List;

public class B {
    private List<Test> a = new ArrayList<Test>();
    //a variable is empty

    public B() {
        // TODO Auto-generated constructor stub
        Test oa = new Test();
        oa.fill(a);// After this method is called, a variable has change with name is "FiFi"
    }

    public static void main(String[] args) {
        new B();
    }
}

And then, I have the same form like that with the object variable, but the result is not the same.

public class Test {
    private String name = "Fifi";
    
    void fill(Test b) {
        b = this;
    }
}
public class B {
    private Test a;
    // so a variable is null

    public B() {
        // TODO Auto-generated constructor stub
        Test oa = new Test();
        oa.fill(a);// after fill method is called, a still null!
    }

    public static void main(String[] args) {
        new B();
    }
}

What is different from Arraylist and object variables like above? I do not understand the basics of how it's happening!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

1 Answers1

-1

ArrayList and Object are two completely different concepts. Arraylist Object

oa.fill(a); // after fill method is called, a still null!

a is null because this

private Test a;

is not an object but a reference of Test class. So no memory is allocated to a. Difference between declaration and instantiation

private Test a = new Test(); //object is created

Difference between your two codes:

This code is appending an object(oa) of Test class to List a-

public class Test {
    private String name = "Fifi";
    
    void fill(List<Test> a) {
        a.add(this);
    }
}

And in this code you are assigning contents of one object(oa) to another object(a) of same class Test-

    public B() {
        Test oa = new Test();
        oa.fill(a);
        System.out.print(a.name); //output: Fifi
    }
avm
  • 387
  • 3
  • 16
  • it wouldn't change anything even if `private Test a;` would be an object, such as `private Test a = new Test();`. The code OP tries still wouldn't work, because assigning to reference passed as value won't work. you would need to use reference as reference. – eis Apr 25 '21 at 11:47
  • @eis Did you run the code? Can you clarify your point after running this code because what you said in the first line isn't consistent with the output. If what you're saying is true then how is the value of `a.name` changing to 'Fifi' after `oa.fill(a)` – avm Apr 25 '21 at 12:36
  • it's not changing, with or without new object creation. it will only change if List is being used, not when Test is used. This is due to the reference usage I explained. – eis Apr 25 '21 at 14:14
  • @eis it is changing with new object creation that's why I asked you to execute the code. – avm Apr 25 '21 at 18:21
  • I'm not sure you understand the problem. here's a runnable demonstration for you showing that it does not change: https://ideone.com/1LNzAw – eis Apr 26 '21 at 06:22
  • @eis I never said that reference will change. Obviously the reference will not change because they are two different objects. OP is asking about value(attributes) change not reference change. I said everything with respect to values. See this- https://ideone.com/zjLBdd – avm Apr 26 '21 at 10:44
  • there's no point discussing since you don't seem to understand the problem. in your example fill() method still does nothing outside the fill method, and object instantiation is irrelevant to the issue. – eis Apr 26 '21 at 18:53