resources.report.rules.pmd.NonThreadSafeSingleton.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sanity4j Show documentation
Show all versions of sanity4j Show documentation
Sanity4J was created to simplify running multiple static code
analysis tools on the Java projects. It provides a single entry
point to run all the selected tools and produce a consolidated
report, which presents all findings in an easily accessible
manner.
NonThreadSafeSingleton
NonThreadSafeSingleton
Non-thread safe singletons can result in bad state changes. Eliminate
static singletons if possible by instantiating the object directly. Static
singletons are usually not needed as only a single instance exists anyway.
Other possible fixes are to synchronize the entire method or to use an
initialize-on-demand holder class (do not use the double-check idiom).
See Effective Java, item 48.
This rule is defined by the following Java class:
net.sourceforge.pmd.rules.design.NonThreadSafeSingleton
Example:
private static Foo foo = null;
//multiple simultaneous callers may see partially initialized objects
public static Foo getFoo() {
if (foo==null)
foo = new Foo();
return foo;
}
This rule has the following properties:
Name Default value Description
checkNonStaticMethods
Do not set this to false and checkNonStaticFields to true
checkNonStaticFields
Do not set this to true and checkNonStaticMethods to false