Method names in Java are generally verbs, but ** static factory methods ** may use prepositions and nouns.
A ** static factory method ** is a method that provides an instance instead of a constructor, such as String.valueOf ()
.
Detailed explanations are delegated to ** "Effective Java" ** and the following articles.
Classes added in Java 8 utilize static factory methods as a way to provide instances.
Optional
Stream
LocalDateTime
Based on the above example, the method name of the static factory method can be generalized as follows.
Method name | role |
---|---|
empty | Create an empty instance |
of | Create an instance of the specified parameter |
from | Create an instance from an object of another class |
In addition to these, it will be easier for the user to give a method name that suits the purpose, such as ʻof Nullableor
now`.
Recommended Posts