Why is this an issue?

A static method named getInstance is the conventional entry point of the singleton pattern. Its name promises the instance of the class, so callers assume that every call hands back the same object and that any state the class holds is shared.

A getInstance method whose body is an unconditional return new Foo(); breaks that promise. No static field is read or written, so nothing is cached and each call creates a separate object. Caches, connection pools, counters and registries owned by the class are then duplicated per call site, and every write performed through one instance is invisible to the holders of the others.

Because each object works perfectly in isolation, the defect does not fail loudly. It surfaces much later, as state that silently goes missing between two unrelated parts of the application. This shape is typically introduced when the caching logic is dropped during a refactoring, or when a class is turned into a singleton by renaming its factory method without adding the field that backs it.

This rule raises an issue when a static no-argument method named getInstance unconditionally returns a newly created instance of its own class, instead of a cached one. Methods that are not named getInstance, and getInstance overloads that take arguments, are out of scope.

The issue is always worth reviewing, but it is not always worth fixing as reported. If the class is stateless and the name is the only problem, renaming the method is enough. If callers are documented as receiving a fresh, independent object, the name is misleading but the behaviour may be deliberate, and renaming is again the fix rather than caching.

How to fix it

Decide which instance the method should return, then make it return that one.

If the class is meant to be a singleton, cache the instance and return that. The usual form is a lazily initialised field, created on the first call and guarded against concurrent access, as in the compliant example below. An eager private static final field is an alternative when construction is cheap and needs nothing that is only known later. In either case, make the constructor private so that no caller can bypass the accessor.

If the class is not meant to be a singleton and callers genuinely need their own object, keep the new and rename the method to getNewInstance, newInstance or create. This fixes the contract instead of the implementation and is the right choice when the class has no state to share.

Caching changes how many objects exist at runtime, so check the callers before applying it. Sharing an instance that was previously per-caller requires the class to be safe to use from several threads, and code that mutates the object after obtaining it, or that relies on object identity, may need to be adapted. Renaming leaves the behaviour unchanged and only updates the contract the name advertises.

Code examples

Noncompliant code example

public class FeatureFlags {

  private final Map<String, Boolean> flags = new HashMap<>();

  public static FeatureFlags getInstance() {
    return new FeatureFlags(); // Noncompliant, every caller gets its own empty map
  }

  public void enable(String name) {
    flags.put(name, true);
  }

  public boolean isEnabled(String name) {
    return flags.getOrDefault(name, false);
  }
}

// FeatureFlags.getInstance().enable("dark-mode");
// FeatureFlags.getInstance().isEnabled("dark-mode"); // returns false, the flag was set on another object

Compliant solution

public class FeatureFlags {

  private static FeatureFlags instance;

  private final Map<String, Boolean> flags = new ConcurrentHashMap<>();

  private FeatureFlags() {
  }

  public static synchronized FeatureFlags getInstance() {
    if (instance == null) {
      instance = new FeatureFlags();
    }
    return instance;
  }

  public void enable(String name) {
    flags.put(name, true);
  }

  public boolean isEnabled(String name) {
    return flags.getOrDefault(name, false);
  }
}

// FeatureFlags.getInstance().enable("dark-mode");
// FeatureFlags.getInstance().isEnabled("dark-mode"); // returns true

Noncompliant code example

A class that is meant to hand out a fresh object on every call still raises an issue when the method is named getInstance. The name, not the new, is what is wrong.

public class Report {

  public static Report getInstance() {
    return new Report(); // Noncompliant, "getInstance" promises one shared object
  }
}

Compliant solution

public class Report {

  public static Report getNewInstance() {
    return new Report();
  }
}

Resources

Documentation

Related rules