Declaring multiple variables in a single declaration may could cause confusion regarding about the types of the variables and their initial values. In particular, do not declare any of the following in a single declaration:
...
In general, you should declare each variable on its own line with an explanatory comment regarding its role. Although it is not required for conformance with this guideguideline, this practice is also recommended in the Code Conventions for the Java Programming Language Conventions 2009 , Section 6.1, "Number Per Line" Conventions 2009.
When more than one variable is declared in a single declaration, ensure that both the type and the initial value of each variable are self-evident.
This guideline applies to:
- local variable declaration statements (JLS §14.4)
- field declarations (JLS §8.3)
- field (constant) declarations (JLS §9.3)
Noncompliant Code Example (
...
Initialization)
This noncompliant code example might lead a programmer or reviewer to mistakenly believe that both i
and j
are initialized to 1. In fact, only j
is initialized; i
remains uninitialized.
Code Block | ||
---|---|---|
| ||
int i, j = 1; |
Compliant Solution (
...
Initialization)
In this compliant solution, it is readily apparent that both i
and j
are initialized to 1.
Code Block | ||
---|---|---|
| ||
int i = 1; // purpose of i... int j = 1; // purpose of j... |
Compliant Solution (
...
Initialization)
In this compliant solution, it is readily apparent that both i
and j
are initialized to 1.
Code Block | ||
---|---|---|
| ||
int i = 1, j = 1; |
Prefer declaring Declaring each variable on a separate line is the prefered method. However, multiple variables on one line are acceptable when they are small temporary variables , such as array indexes.
Noncompliant Code Example (
...
Different Types)
In this noncompliant code example, the programmer declares multiple variables, including an array, on the same line. All instances of the type T
have access to methods of the Object
class Object
. However, it is easy to forget that arrays require special treatment when some of these methods are overridden.
Code Block | ||
---|---|---|
| ||
public class Example<T> { private T a, b, c[], d; public Example(T in) { a = in; b = in; c = (T[]) new Object[10]; d = in; } } |
When a an Object
method of Object
, such as toString()
is overridden, a programmer might could accidentally provide an implementation for type T
that fails to consider that c
is an array of T
, rather than a reference to an object of type T
.
...
However, the programmer's actual intent might could have been to invoke toString()
on each individual element of the array c
.
Code Block |
---|
// Correct functional implementation public String toString(){ String s = a.toString() + b.toString(); for(int i = 0; i < c.length; i++){ s += c[i].toString(); } s += d.toString(); return s; } |
Compliant Solution (
...
Different Types)
This compliant solution not only places each declaration on its own line and, it also uses the preferred notation for array declaration.
...
DCL01-EX1: Note that the declaration of a loop counter in a for statement is in violation of this recommendation guideline because the declaration is not on its own line with an explanatory comment about the variable's role of the variable. However, declaration of loop indices in for statements is not only a common idiom; it also provides the benefit of restricting the scope of the loop index to that of the for loop itself. These are sufficient reasons This is a specific reason to relax this guideline in this specific case.
Declarations of loop indices should be included within a for statement:
...