A static getInstance() creates a new object on every call

tx0/java:fresh-get-instancerecommendedwarningbug

What it finds

A static getInstance() reads as the accessor of a single shared instance. One that returns new Config() every time hands each caller its own copy, so state set through one is missing in the others, and the cost of creating it is paid on every call.

Reported is a static getInstance() without parameters whose body only returns a new instance of its own class.

From the rule's source, fresh_get_instance.rs.

Example from the rule's tests
class Config {  private static final Config INSTANCE = new Config();   static Config getInstance() {    return new Config();tx0 reports: creates a new object on every call  }} class Registry {  private static final Registry INSTANCE = new Registry();   static Registry getInstance() {    return INSTANCE;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:fresh-get-instance" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:fresh-get-instance] reason="..."

tx0 explain tx0/java:fresh-get-instance shows the same in the terminal. Found a false positive? Report it.