This question already has an answer here:
- What are static factory methods in Java? 14 answers
I recently picked up Java and I was exploring the java.time
package and noticed a, in my view, very nice interface where I could do ZonedDateTime.of(2015, 12, 1, …)
. Instead of the classical new ZonedDateTime(2015, 12, 1, …)
. I saw the same with a .from
method to create the object from another object.
But just looking at the implementation without understanding why it's sometimes of
and sometimes from
I feel that I'm missing something. Especially since I'm now keen on using the same pattern for some of my own classes.
This being Java I'm assuming that there's an actual formal proper name for this, and not just something that the implementors of the java.time
package made on their own. :)
Aucun commentaire:
Enregistrer un commentaire