Hiding a constructor behind a static creator method?

前端 未结 9 1642
我在风中等你
我在风中等你 2021-01-04 00:47

I\'ve recently discovered an interesting way to create a new instance of an object in Google Guava and Project Lombok: Hide a constructor behind a static creator method. Thi

9条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 01:18

    Some main reasons

    • Primarily it gives you the power to instantiate a different (sub) class
    • Possibility to return null
    • It enables you to return an already existing object

提交回复
热议问题