Field Name Matches Class Name

ID

java.field_name_matches_class

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

code-style, naming

Description

Reports fields whose name (case-insensitive) matches the enclosing class name. This creates confusion between the field and the type, making the code harder to read and maintain.

Rationale

When a field has the same name as its enclosing class, expressions like item.item or User.user become ambiguous at a glance. It also makes auto-completion less helpful and can confuse code-generation tools.

// Bad -- field name matches class
class Item {
    String item;    // confusing: item.item
}

// Good -- descriptive field name
class Item {
    String name;
}

Remediation

Rename the field to a more descriptive name that does not collide with the class name.