java - Constructor or Setters? -


well of may simple question i'm asking ensure class design isn't violating design concepts when comes proper use of constructor access modifier.

in class i'm working on didn't use setters because thought correct initialize subject object using constructor instead of setting each field using setter methods. hence, require other teammates supply attributes of class subject when instantiated before calling add() method.

my question why i'm getting warning on netbeans?

enter image description here

  1. should make instance variables private final?

  2. should remove constructor parameters , create setter methods initialize fields/variables?

here's code.

public class subject {      private string subjectname;     private string subjectcode;     private int subjectunits;     private string subjectdescription;     private string subjectyearlevel;     private int schoolyearstart;     private int schoolyearend;      public subject(string name, string code, int units, string description, string yearlevel){         subjectname = name;         subjectcode = code;         subjectunits = units;         subjectdescription = description;         subjectyearlevel = yearlevel;     }      public void add(){          string sql = "insert subject(name,code,units,description,yearlevel,creator) "                 + "values (?,?,?,?,?,?)";         try(connection con = dbutil.getconnection(dbtype.mysql);                 preparedstatement ps = con.preparestatement(sql);){             ps.setstring(1,subjectname );             ps.setstring(2,subjectcode );             ps.setint(3, subjectunits);             ps.setstring(4, subjectdescription);             ps.setstring(5, subjectyearlevel);             ps.setstring(6, login.getusername());          } catch (sqlexception e) {             joptionpane.showmessagedialog(null,e.getclass()+" "+e.getmessage());         }     } } 

i'd appreciate advice.

thanks.

netbeans giving warning because don't have method changes original value of fields, netbeans recommending in order make code more readable. hence if add method modifies original value stop showing it.

about design:

the decission between inmutable , mutable objects it's purpose of objects.

according example:

if subject class it's going change in bussines logic , changes going have further use, in fact if subject holds identity it's preferable use mutable class design, doesn't need implemented setters, can use factory.

otherwise if object doesn't have identity , pro-performance instantiate new object every time need different 1 use inmutable, applies concurrency, since inmutable objects thread-safe.

as example hibernate, java orm, need models mutable , have setter methods.

if want read more it:

6 benefits of programming immutable objects in java
if immutable objects good, why people keep creating mutable objects?

j. pichardo


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -