Inspectopedia 2025.2 Help

Magic number

Reports "magic numbers": numeric literals that are not named by a constant declaration.

Using magic numbers can lead to unclear code, as well as errors if a magic number is changed in one location but remains unchanged not another. The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100, 1000, 0L, 1L, 2L, 0.0, 1.0, 0.0F and 1.0F are not reported by this inspection.

Example:

void checkFileSize(long bytes) { if (bytes > 1_048_576) { throw new IllegalArgumentException("too big"); } }

A quick-fix introduces a new constant:

static final int MAX_SUPPORTED_FILE_SIZE = 1_048_576; void checkFileSize(long bytes) { if (bytes > MAX_SUPPORTED_FILE_SIZE) { throw new IllegalArgumentException("too big"); } }

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.

MagicNumber
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 | Abstraction issues

Configure the inspection:

  • Use the Ignore constants in 'hashCode()' methods option to disable this inspection within hashCode() methods.

  • Use the Ignore in annotations option to ignore magic numbers in annotations.

  • Use the Ignore initial capacity for StringBuilders and Collections option to ignore magic numbers used as initial capacity when constructing Collection, Map, StringBuilder or StringBuffer objects.

Inspection options

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

Ignore constants in 'hashCode()' methods

Default value:

Selected
Ignore in annotations

Default value:

Selected
Ignore initial capacity for StringBuilders and Collections

Default value:

Not selected

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection MagicNumber

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.2, Qodana for JVM 2025.2,

Last modified: 18 September 2025