java - In a getter method, Should I clone a String being returned? -
in java, advisable create string when returning fields strings in getter method. my_name string field of class.
public string getname() { string rstring = my_name.tostring(); homecoming rstring; }
in java, advisable create string when returning fields strings in getter method. my_name string field of class.
no, there no point in doing so. since strings in java immutable, when seek modify string returned getter
method (i.e. re-assign string reference pointing string object, different string), create new string, , assign reference string current string reference, hence detaching original string.
however, if have mutable
fields, array
, in case, advisable homecoming defensive re-create of it, if want object immutable.
java string field getter-setter cloning
No comments:
Post a Comment