Java Object reference -
this regarding usage of arraylist returned class instance variable.
class { //assigned list of string it. private list < string > newal; //returns list public list < string > getlist() { return newa1; } } class test { public void go() { list < string > list = a.getlist(); list.add(""); } }
in test class when retreive list , manipulate list.because of reference ,class list got manipulated.if part of third party code.how correct code in test class original object wouldnt affected?
the arraylist constructor takes collection can use that:
list<string> list = new arraylist(a.getlist());
i think it's better this, depending on you're doing, may want construct new list in getter. helps type hiding.
Comments
Post a Comment