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

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -