Difference between calling new and getInstance()

前端 未结 6 1829
一个人的身影
一个人的身影 2021-01-31 11:30

Is calling Class.getInstance() equivalent to new Class()? I know the constructor is called for the latter, but what about getInstance()? T

6条回答
  •  深忆病人
    2021-01-31 12:31

    Absolutely (usually) not.

    getInstance is the static method often used with the Singleton Pattern in Java. The new keyword actually creates a new object. At some point there must be a new (although there are a few other methods to instantiate new objects) to actually create the object that getInstance returns.

提交回复
热议问题