Inspectopedia Help

Constant values

Reports expressions and conditions that always produce the same result, like true, false, null, or zero. Such expressions could be replaced with the corresponding constant value. Very often though they signal about a bug in the code.

Examples:

// always true // root cause: || is used instead of && if (x > 0 || x < 10) {} System.out.println(str.trim()); // always false // root cause: variable was dereferenced before null-check if (str == null) {}

The inspection behavior may be controlled by a number of annotations, such as nullability annotations, <a href="https://www.jetbrains.com/help/idea/contract-annotations.html">@Contract</a> annotation, @Range annotation and so on.

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.

ConstantValue
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 | Probable bugs

Configure the inspection:

  • Use the Don't report assertions with condition statically proven to be always true option to avoid reporting assertions that were statically proven to be always true. This also includes conditions like if (alwaysFalseCondition) throw new IllegalArgumentException();.

  • Use the Ignore assert statements option to control how the inspection treats assert statements. By default, the option is disabled, which means that the assertions are assumed to be executed (-ea mode). If the option is enabled, the assertions will be completely ignored (-da mode).

  • Use the Warn when constant is stored in variable option to display warnings when variable is used, whose value is known to be a constant.

Before IntelliJ IDEA 2022.3, this inspection was part of "Constant Conditions & Exceptions" inspection. Now, it split into two inspections: "Constant Values" and "Nullability and data flow problems".

Inspection options

Here you can find the description of settings available for the Constant values inspection, and the reference of their default values.

Don't report assertions with condition statically proven to be always true

Not selected

Ignore assert statements

Not selected

Warn when constant is stored in variable

Default: Selected

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Java, 241.18072

Last modified: 18 June 2024