again I stepped over some old code and did a SonarLint analysis. I can't paste the code here, but basically it looks like:
@SuppressWarnings("static-access") public class SuperClass { private SuperClass() { } public static SuperClass getInstance() { return InstanceHolder.instance; } private static class InstanceHolder { public final static SuperClass instance = new SuperClass(); } public void doSomething(){
SonarQube (sonar-java: 4.2.1.6971) reports a problem on S1118.
Adding a private constructor to InstanceHolder has no effect here, since SuperClass is the only class that can instantiate because of its private modifier.
SuperClass can instantiate even if รnstanceHolder has a private constructor.
BTW: adding a constructor fixes the sonar problem, so I think the analyzer noted this violation of the rule, because "UtilityClass" - without further study.
Have you decided that this is a mistake? Instead of a design flaw, this is an example of a thread-safe singleton.
source share