Inspectopedia Help

Accessing a non-public field of another object

Reports accesses to private or protected fields of another object. Java allows access to such fields for objects of the same class as the current object but some coding styles discourage this use. Additionally, such direct access to private fields may fail in component-oriented architectures, such as Spring or Hibernate, that expect all access to other objects to be through method calls so the framework can mediate access using proxies.

Example:

public class Base { protected int bar; void increment(Base base) { bar++; base.bar++; // warning: direct access to another object's non-public field } }

A quick-fix to encapsulate the field is available.

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.

AccessingNonPublicFieldOfAnotherObject
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 | Encapsulation

Configure the inspection:

  • Use the Ignore accesses from the same class option to ignore access from the same class and only report access from inner or outer classes.

    To ignore access from inner classes as well, use the nested Ignore accesses from inner classes.

  • Use the Ignore accesses from 'equals()' method to ignore access from an equals() method.

Inspection options

Here you can find the description of settings available for the Accessing a non-public field of another object inspection, and the reference of their default values.

Ignore accesses from the same class

Not selected

Ignore accesses from 'equals()' method

Not selected

Inspection Details

By default bundled with:

IntelliJ IDEA 2024.2, Qodana for JVM 2024.2,

Can be installed with plugin:

Java, 242.22892

Last modified: 11 September 2024