Java Elements in List not "automatically updating" -


first of all: forgot term "automatically updating", mean this:

int x = 5; int y = x; system.out.println(y); //prints 5 x = 3; system.out.println(y); //now prints 3! 

(note using reference: above example wrong addressed in comments.)

however tried approach on list, , not work, 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 have example account account = new account("test", "1");, , when use system.out.println(account.getdata()) prints [1], still correct. when execute account.setpassword("11"); , subsequently system.out.println(account.getdata()) still prints [1] instead of expected [11].

i expected data updated automatically x = y point same memory location.

any guess going on? error in implementation? or feature? , how can efficiently work around this?

regards.

edit: changed setpassword() below code, should work:

public void setpassword(final string password) {     int index = data.indexof(this.password);     this.password = password;     data.set(index, password); } 

however wonder, there not better solution this? requires 3 lines of code of 2 of them can forgotten.

i think you're confused. code:

int x = 5; int y = x; system.out.println(y); //prints 5 x = 3; system.out.println(y); //now prints 3! 

... not print 3. assignment:

int y = x; 

... doesn't associate y , x variables each other. just copies current value of x y. further changes x not reflected in y.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -