Avoid instantiating a class in java

前端 未结 5 1703
死守一世寂寞
死守一世寂寞 2020-12-04 18:32

Recently I\'ve faced a question : How to avoid instantiating a Java class?

However, I answered by saying:

  1. If you don\'t want to instantiate

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 18:44

    Sometimes you only want to avoid others instantiating your objects, in order to have full control over all existing instances. One example is the singleton pattern.

提交回复
热议问题