...
In general, you should declare each variable on its own line with an explanatory comment regarding its role. Although it is While not required for conformance with this guideline, this practice is also recommended in the Code Conventions for the Java Programming Language, §6.1, "Number Per Line" [Conventions 2009].
...
- Local variable declaration statements [JLS 20112013, §14.4]
- Field declarations [JLS 20112013, §8.3]
- Field (constant) declarations [JLS 20112013, §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; , while i
remains uninitialized:
Code Block | ||
---|---|---|
| ||
int i, j = 1; |
...
Code Block |
---|
public String toString() { return a.toString() + b.toString() + c.toString() + d.toString(); } |
...
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;
}
|
...
Such declarations are not required to be in a separate line; , and the explanatory comment may also be omitted.
Bibliography
§6.1, "Number Per Line" | |
[ESA 2005] | Rule 9, Put Single Variable Definitions in Separate Lines |
§4.3.2, "The |
...