3 ms·
Never had a need for this. static factory methods are almost always the better solutions. Ctors should only assign params to fields and nothing more. Though I
by oweiler 3y ago
Never had a need for this. static factory methods are almost always the better solutions.
Ctors should only assign params to fields and nothing more.
Though I applaud that it makes the language more intuitive/consistent.
- BlackFly 3y agoI would often use static factory methods as well (next to the other patterns given in the JEP itself), but it breaks the usual convention of using new, so it adds a bit of cognitive load to learning an API where you expect to be able to use new, nope that's private, so you try build(), construct(), etc. before finally reading the documents to figure out what the name of the static method is, oh it's initialize(). Constructors in java should enforce class invariants in my opinion. Otherwise we should just have data only record initializers like the new record classes have. I expect a constructor for BigPositiveInteger to never be able to construct a negative integer.
- nh23423fefe 3y agoNo. Constructors turn argument lists into instances. You can't find a "should" anywhere.