heisenberg
heisenberg

Reputation: 1954

Constructor or Setters?

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?

enter image description here

  1. Should I make the instance variables private final?

  2. 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.

Upvotes: 1

Views: 326

Answers (3)

Necromancer
Necromancer

Reputation: 919

The warning is showed because you initialized the attributes using only constructor. And then never changed.

This warning will show at NetBean and will not show at Eclipse.

I think you can write your class like this :

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){
    setName(name);
    setCode(code);
    setUnits(units);
    /*other setters*/
}

public String getSubjectYearLevel() {
     return subjectYearLevel;
}

public void setSubjectYearLevel(String subjectYearLevel) {
      /*Some validation and filters can write there*/
      this.subjectYearLevel = subjectYearLevel;
}

/*other getter setter.....*/


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());
    }
}

}

In setter, you can write some validation codes of your attribute for someone doesn't know about your class.

Upvotes: 0

Akash
Akash

Reputation: 74

Since you have initialized your instance variables in constructor. And you have not reinitialized variable. Constructor is only going to call once in object life cycle. So netbeans suggesting you to declare these variables as final. If you use setter method then this warning will not be there. Because method can be called more than once, which is reinitialization of variables.

Upvotes: 1

J. Pichardo
J. Pichardo

Reputation: 3115

Netbeans is giving that warning because you don't have any method that changes the original value of those fields, so Netbeans is recommending that in order to make your code more readable. Hence if you add any method that modifies that original value it will stop showing it.

About the design:

The decission between Inmutable and Mutable Objects it's about the purpose of those objects.

According to your example:

If your Subject class it's going to change in the Bussines Logic and those changes are going to have further use, in fact if the Subject holds an identity it's preferable to use a mutable class design, it doesn't need to be implemented with setters, it can also use a Factory.

Otherwise if your object doesn't have an identity and it is pro-performance to instantiate a new object every time you need a different one then use Inmutable, this also applies for concurrency, since inmutable objects are thread-safe.

As an example Hibernate, a Java ORM, need the models to be mutable and have setter methods.

If you want to read more about it:

6 Benefits of Programming with Immutable Objects in Java
If immutable objects are good, why do people keep creating mutable objects?

J. Pichardo

Upvotes: 1

Related Questions