Why LocalDate
, LocalTime
, Stream
, etc. objects use a factory method of()
instead of a constructor?
I found an explanation of why a factory methods should be used instead of new
here. This answer gives a number of reasons, but the only thing that is relevant to Java Date/Time API is the following:
unlike constructors, they are not required to create a new object each time they’re invoked
As LocalDate
and LocalTime
are immutable, it probably makes sense to use a factory and reuse existing objects instead of creating a new object every time.
Is it the reason why objects like LocalDate
and LocalTime
are created with a factory method (i.e., LocalDate.of()
)? Are there any other reasons?
Also, Stream
objects are mutable. Why a factory method (Stream.of()
) is used to create a Stream
?