Well for the most of you this may be a simple question but I'm asking this to ensure that my class design isn't violating design concepts when it comes with proper use of constructor as well as access modifier.
In this class I'm working on I didn't use setters because I thought it would be correct to initialize Subject object by using the constructor instead of setting each field using setter methods. Hence, require my other teammates to supply the attributes of class Subject when it is instantiated before calling add()
method.
My question is why I'm getting this warning on Netbeans?
Should I make the instance variables
private final
?Should I just remove the constructor parameters and create setter methods to initialize the fields/variables?
Here's my 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 INTO 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 any advice.
Thanks.