Paul Clapham wrote:So now you're going to have "private String specialErrorMessage" in there as well as the other three types of message?
I'd suggest stop making "errorMessage" be handled differently than the other message types. Have only one constructor which takes no parameters and have all of the data fields set by "setXXXMessage" methods.
You could also have the "setXXXMessage" methods return "this" instead of void, so you could chain them together:
Then you could write
Junilu is right; make the class and both its fields final, so it will be immutable. That way there will be no risk of users forgetting to set the message. You don't want people to use your Message object and have a method return null.Junilu Lacar wrote:. . . Do it all in the constructor and make the class immutable. . . .
Junilu Lacar wrote:I concur with Lou's suggested approach, but without the setter for message type. Do it all in the constructor and make the class immutable.
Also, consider renaming the class to simply Message: I don't see any reason to name it "MessageReturn" - you're probably thinking "It's a massage that I return, so 'MessageReturn'". No. It's a message plain and simple. The fact that you return it from a method is irrelevant to the name choice.
But a record always has getters; they are simply named differently.Lou Hamers wrote:. . . Get rid of the getters too!!! . . .