Java – guava: why method create () instead of constructor?

Excuse me, can you explain why guava creators like to define constructors as private and define static methods create () to create objects?

Solution

Valid java project 1: consider static factory methods instead of constructors

Some advantages of the static factory approach include:

>They automatically infer pre Java 7 type parameters. > They allow us to control inheritance patterns: we can subclass collection types in guava without having external code classify them. > They let us return any subclass of the desired type, allowing us to better hide the implementation details

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>