mercredi 16 décembre 2015

Can not assign final fields with builder pattern

I am trying to implement a DTO following the builder pattern, but I have an error.

The question:

The final field SearchRequestDTO.requestType cannot be assigned

Why am getting the following error in the private constructor of SearchRequestDTO:

The code:

public class SearchRequestDTO {

   private final String requestType = null;
   private final String requestSubType = null;

    private SearchRequestDTO(SearchRequestDTOBuilder builder){
        this.requestType = builder.requestType ;
    }

    public String getRequestType() {
        return requestType;
    }

    public String getRequestSubType() {
        return requestSubType;
    }

public static class SearchRequestDTOBuilder {
    private String requestType = null;
    private String requestSubType = null;

    public SearchRequestDTOBuilder requestType(String requestType){
        this.requestType = requestType;
        return this;
    }
    public SearchRequestDTOBuilder requestSubType(String requestSubType){
        this.requestSubType = requestSubType;
        return this;
    }

    public SearchRequestDTO build(){
        return new SearchRequestDTO(this);
    }
}

Aucun commentaire:

Enregistrer un commentaire