Where should calls to validate a Java object be made; 1) at the end of the constructor, or 2) after the call to new X()? If made in the constructor, then is gets called not only for each new, but if there is a subclass of that object. Your thoughts would be helpful.
Where should calls to validate a Java object be made; 1) at the end of the constructor, or 2) after the call to new X()? If made in the constructor, then is gets called not only for each new, but if there is a subclass of that object. Your thoughts would be helpful.
Share Improve this question asked Feb 6 at 2:56 B. StackhouseB. Stackhouse 5777 silver badges17 bronze badges1 Answer
Reset to default 0It completely depends on the use case and logic. I prefer to call it after new X()
as object will have data after some setters are called.
X obj = new X();
obj.setName("xyz");
obj.setId(1);
validator.validate(obj);