programming languages - Java setter method convention (is it OK to overload setters?) -
i have condition way construct string (finalvalue) based on number of non null values in input. wondering if ok on load setter method string (finalvalue) 1 diff no of parameters , call them based on values get? bad programming practice?
public void setfinalstring(string a){ this.finalstring = a; } public void setfinalstring(string a, string b){ this.finalstring = + " --f " + b; }
or can have method construct finalstring based on inputs , invoke setter (no overloading here) finalstring.
pls tell me ok on load setters , suggest better approach?
thanks
yes, bad approach, setter supposed set passed parameter encapsulated private ivar.
the other logic should somewhere else not in setter, although accepted have restrictions when set parameter in setter i.e.
public setage(int age) { if (age >= 0) this.age = age; else this.age = 0; }
that logic setter should have, should never receive more value assigning ivar.
Comments
Post a Comment