Avoid instantiating a class in java

前端 未结 5 1699
死守一世寂寞
死守一世寂寞 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:47

    I think the most common reason for not wanting to instantiate a class is when you are dealing with a static class, and therefore with its static methods. You don't want someone to try to instantiate that class. Likewise, when you are dealing with Factory classes or in some cases many singleton classes will hide their constructor as to not be instantiated in the normal way.

提交回复
热议问题