Inspectopedia 2024.3 Help

Synchronization on an object initialized with a literal

Reports synchronized blocks that lock on an object initialized with a literal.

String literals are interned and Character, Boolean and Number literals can be allocated from a cache. Because of this, it is possible that some other part of the system, which uses an object initialized with the same literal, is actually holding a reference to the exact same object. This can create unexpected dead-lock situations, if the lock object was thought to be private.

Example:

class Main { final String mutex = "Mutex"; void method() { synchronized (mutex) { } } }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

SynchronizedOnLiteralObject
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Threading issues

Use the Warn on all possible literals option to report any synchronization on String, Character, Boolean and Number objects.

Inspection options

Here you can find the description of settings available for the Synchronization on an object initialized with a literal inspection, and the reference of their default values.

Warn on all possible literals

Not selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.3, Qodana for JVM 2024.3,

Can be installed with plugin:

Java, 243.23126

Last modified: 03 December 2024