Java Elements in List not "automatically updating"
<p>First of all: I forgot the term for "automatically updating", but I mean this:</p>

<pre><code>int x = 5;
int y = x;
System.out.println(y); //Prints 5
x = 3;
System.out.println(y); //Now prints 3!
</code></pre>

<p>(<strong>NOTE for anyone using this as reference: Above example is wrong as addressed in comments.</strong>)</p>

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

<p>Relevant part of Account.java:</p>

<pre><code>public class Account extends Entity&lt;String, Account&gt; {
    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;
    }
}
</code></pre>

<p>Relevant part of Entity.java:</p>

<pre><code>abstract public class Entity&lt;K, D&gt; {
    protected K key;
    protected List&lt;Object&gt; data;

    public Entity() {
        data = new ArrayList&lt;&gt;();
    }

    public K getKey() {
        return key;
    }

    public List&lt;Object&gt; getData() {
        return data;
    }

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

        list.addAll(data);

        if (dataAction == DataAction.UPDATE) {
            list.add(key);
        }
        return list;
    }
}
</code></pre>

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

<p>I expected that <code>data</code> would be updated automatically just as <code>x = y</code> point to the same memory location.</p>

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

<p>Regards.</p>

<p><strong>EDIT:</strong> Changed <code>setPassword()</code> to below code, now it should work:</p>

<pre><code>public void setPassword(final String password) {
    int index = data.indexOf(this.password);
    this.password = password;
    data.set(index, password);
}
</code></pre>

<p>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.</p>
