Java Singleton vs static - is there a real performance benefit?

前端 未结 7 2028
抹茶落季
抹茶落季 2021-02-04 06:46

I am merging a CVS branch and one of the larger changes is the replacement wherever it occurs of a Singleton pattern with abstract classes that have a static initialisation bloc

7条回答
  •  遥遥无期
    2021-02-04 06:56

    If my original post was the correct understanding and the discussion from Sun that was linked to is accurate (which I think it might be), then I think you have to make a trade off between clarity and performance.

    Ask yourself these questions:

    1. Does the Singleton object make what I'm doing more clear?
    2. Do I need an object to do this task or is it more suited to static methods?
    3. Do I need the performance that I can gain by not using a Singleton?

提交回复
热议问题