Declaring multiple variables in a single declaration could cause confusion about the types of variables and their initial values. In particular, do not declare any of the following in a single declaration:
- variables Variables of different types
- a A mixture of initialized and uninitialized variables
...
- local variable declaration statements [Java 2011, §14.4],
- field declarations [Java 2011, §8.3],
- field (constant) declarations [Java 2011, §9.3],
Noncompliant Code Example (Initialization)
...
Declarations of loop indices should be included within a for
statement even if that it results in variable declarations that do not include a comment about the purpose of the variable:
Code Block | ||
---|---|---|
| ||
public class Example { void function() { int mx = 100; // someSome max value for (int i = 0; i < mx; ++i ) { /* ... */ } } } |
Such declarations are not required to be in a separate line, and the explanatory comment may also be omitted.
...
Section 6.1, "Number Per Line" | |||
[ESA 2005] | Rule 9: Put single variable definitions in separate lines., Put Single Variable Definitions in Separate Lines | ||
[JLS 2011] | §4.3.2, "The class Object" |
| |
| |||
| §4.3.2, "The class Object" |
...