Java – Best way to invoke ‘setter method’ for first access and ‘getter method’ for the rest with “getter setter” pattern

designdesign-patternsjavaobject-orientedserialization

Here is a json which comes in the request param. I am constructing a class with getter and setter for accessing the values in json so that I could be able to pass the class object to different methods and able to access the member variables in them.

public class RequestParams {
    private String student_id;
    private String student_name;
    private String student_role_number;
    private String department_name;
    private String stream;

    private JSONObject studentDetails;

    public RequestParams(HttpServletRequest request) {
        this.studentDetails = request.getParameter(“studentdetails”);
    }

    public String getStudentId() {
        if(this.student_id == null) {
            this.setStudentId();
        }
        return this.student_id;
    }
    public String getStudentName() {
        if(this.student_name == null) {
            this.setStudentName();
        }
        return this.student_name;
    }
    public String getRoleNumber() {
        if(this.student_role_number == null) {
            this.setRoleNumber();
        }
        return this.student_role_number;
    }
    public String getDepartmentName() {
        if(this.department_name == null) {
            this.setDepartmentName();
        }
        return this.student_name;
    }
    public String getStream() {
        if(this.stream == null) {
            this.setStream();
        }
        return this.stream;
    }
    public void setStudentId() {
        this.student_id = this.studentDetails.getString("student_id");
    }
    public void setStudentName() {
        this.student_name = this.studentDetails.getString("student_name");
    }
    public void setRoleNumber() {
        this.student_role_number = this.studentDetails.getString("role_number");
    }
    public void setDepartmentName() {
        this.department_name = this.studentDetails.getString("department_name");
    }
    public void setStream() {
        this.stream = this.studentDetails.getString("stream");
    }
}

Have the following doubts,

  1. Constructing as class object to reference it from different methods – Is this a good one? Am I going wrong?
  2. How to organise my getter setter so that only set is called only for the first time and for the next calls the value is returned directly? Is there a better way to avoid the null check each time

if(this.student_id == null) {

this.setStudentId();

}

  1. Is there any advantage of accessing the methods and variables within the class with this. ?

PS: I could not invoke all the setter initially from the constructor because all the values declared in the class need not be necessarily present in the json. So, I thought that it would be better if I could initialise the member variable with value during first access.

Best Answer

Your member variables are redundant. You should either store the JSONObject or the Strings but not both of them. This will avoid consistency issues.

Here is a suggestion:

public class RequestParams {
    private final String student_id;
    private final String student_name;
    private final String student_role_number;
    private final String department_name;
    private final String stream;

    public RequestParams(HttpServletRequest request) {
        JSONObject studentDetails = request.getParameter(“studentdetails”);
        this.student_id = studentDetails.getString("student_id");
        this.student_name = studentDetails.getString("student_name");
        this.student_role_number = studentDetails.getString("role_number");
        this.department_name = studentDetails.getString("department_name");
        this.stream = studentDetails.getString("stream");
    }

    public String getStudentId() {
        return this.student_id;
    }
    public String getStudentName() {
        return this.student_name;
    }
    public String getRoleNumber() {
        return this.student_role_number;
    }
    public String getDepartmentName() {
       return this.student_name;
    }
    public String getStream() {
        return this.stream;
    }
}

Note that the member variables can be made final and the class is immutable now.

Related Topic