Method name of static factory method learned from Java 8

Method names in Java are generally verbs, but ** static factory methods ** may use prepositions and nouns.

What is a static factory method?

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.

Java 8 usage example

Classes added in Java 8 utilize static factory methods as a way to provide instances.

Optional

Stream

LocalDateTime

Generalization

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 Nullableornow`.

Reference link

Recommended Posts

Method name of static factory method learned from Java 8
Benefits of Java static method
Significance of interface learned from Java Collection
static factory method part 1
Method name of method chain in Java Builder + α
Java beginner design pattern (Factory Method pattern)
Search and execute method by name from instance with processing (java)
[Java] Wrapper that executes private method of object from outside class
Implementation of clone method for Java Record
Name a group of regular expressions (Java)
Java method
Java static
[Read Effective Java] Chapter 2 Item 1 "Consider static factory methods instead of constructors"
Call Java method from JavaScript executed in Java
Java method
[Java] method
What I learned from Java monetary calculation
[Java] method
Increment with the third argument of iterate method of Stream class added from Java9
[Android] Call Kotlin's default argument method from Java
[Java] Handling of JavaBeans in the method chain
[Java] Dynamic method call by reflection of enum (enum)
Java method call from RPG (method call in own class)
I tried hitting a Java method from ABCL
A quick review of Java learned in class
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Java static story
Factory Method Pattern
Java8 method reference
[Java] forEach method
Factory Method pattern
java8 method reference
[Java] Random method
[Java] Overview of Java
[Java] split method
[Java] How to use compareTo method of Date class
[Java] Meaning of Public static void main (String [] args)
A quick review of Java learned in class part4
How to write Scala from the perspective of Java
Include image in jar file with java static method
[Java] How to extract the file name from the path
Call a method with a Kotlin callback block from Java
Java language from the perspective of Kotlin and C #
A quick review of Java learned in class part3
A quick review of Java learned in class part2
Deep Learning Java from scratch 6.2 Initial values of weights
Why implement with a singleton instead of a static method
Summary of file reading method for each Java file format