Search code examples
javajakarta-eesingletonpmd

@Singleton class do I have to make constructor private


I'm writing a Singleton class in Java EE 6 using the @Singleton annotation. I have not declared a constructor.

Do I have to create a private constructor explicitly?

The reason I'm asking is that I'm using PMD and the rule UseSingleton is flagged. I think that PMD does not understand the annotation @Singleton?


Solution

  • PMD is recommending making the constructor private so that others may not instantiate your singleton object. It's unaware of Java EE 6 annotations.

    You can safely ignore this warning.