Java: getInstance vs Static

后端 未结 6 823
清歌不尽
清歌不尽 2021-01-30 11:17

What is the purpose of getInstance() in Java?

During my research I keep reading that getInstance() helps achieve a Singleton design pattern (w

6条回答
  •  时光取名叫无心
    2021-01-30 11:57

    I prefer to use static too, but sometimes getInstance() is helpful to have some functions that will be related to the object, in which you can modify variables. if you are simply making some util functions that do not need an instance of an object, use static.

    When you are using someone's libraries, you never know if a function body needs a class instance. That's why a lot of library classes are using getInstance().

提交回复
热议问题