First of all: I forgot the term for "automatically updating", but I mean this:

```
int x = 5;
int y = x;
System.out.println(y); //Prints 5
x = 3;
System.out.println(y); //Now prints 3!

```

(**NOTE for anyone using this as reference: Above example is wrong as addressed in comments.**)

However I tried this approach on a List, and it does not work, my code:

Relevant part of Account.java:

```
public class Account extends Entity<String, Account> {
    private String username;
    private String password;

    public Account(final String username, final String password) {
        this.username = username;
        this.password = password;

        key = username;
        data.add(password);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(final String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }
}

```

Relevant part of Entity.java:

```
abstract public class Entity<K, D> {
    protected K key;
    protected List<Object> data;

    public Entity() {
        data = new ArrayList<>();
    }

    public K getKey() {
        return key;
    }

    public List<Object> getData() {
        return data;
    }

    protected List<Object> createData(final DataAction dataAction) {
        List<Object> list = new ArrayList<>();
        if (dataAction == DataAction.INSERT) {
            list.add(key);
        }

        list.addAll(data);

        if (dataAction == DataAction.UPDATE) {
            list.add(key);
        }
        return list;
    }
}

```

Whenever I have for example this `Account account = new Account("test", "1");`, and then when I use `System.out.println(account.getData())` it prints `[1]`, that still is correct. But when I execute `account.setPassword("11");` and then subsequently `System.out.println(account.getData())` it still prints `[1]` instead of the expected `[11]`.

I expected that `data` would be updated automatically just as `x = y` point to the same memory location.

Any guess what is going on? Error in implementation? Or feature? And how can I efficiently work around this?

Regards.

**EDIT:** Changed `setPassword()` to below code, now it should work:

```
public void setPassword(final String password) {
    int index = data.indexOf(this.password);
    this.password = password;
    data.set(index, password);
}

```

However I wonder, is there not a better solution to this? As it requires three lines of code now of which two of them can easily be forgotten.