Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

...

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
bgColor#ccccff
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.

...

[Conventions 2009]

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"
§6.1, "Declarations"
§8.3, "Field Declarations"
 
§9.3, "Field (Constant) Declarations"

 

§14.4, "Local Variable Declaration Statements"

 

§6.1, "Declarations"

 

§4.3.2, "The class Object"

 

...