One Variable per Declaration

ID

java.one_variable_per_declaration

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

code-style

Description

Reports local variable or field declarations that declare more than one variable in a single statement.

Rationale

Declaring multiple variables in one statement (e.g. int a = 1, b = 2, c;) hurts readability in several ways: it is easy to overlook that some variables are uninitialized while others are not, the types may be confusing when arrays are involved (int[] a, b declares a as an array but b as a plain int in C-style), and version-control diffs become noisier when a single variable is added or removed from the list.

// Bad — multiple variables in one declaration
int x = 1, y = 2, z;

// Bad — multiple fields
private String name = "a", label = "b";

Remediation

Declare each variable in its own statement:

// Good — one variable per declaration
int x = 1;
int y = 2;
int z;

// Good — one field per declaration
private String name = "a";
private String label = "b";