Versions Compared

Key

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

Declaring multiple variables in a single declaration may cause confusion regarding the types of the variables and their initial values. In particular do not declare any of the following in a single declaration:

  • variables or fields and functionsvariables or fields of different types
  • a mixture of initialized and uninitialized variables or fields

In general, you should declare each variable on its own line with an explanatory comment about the role of the variable. Although not required for conformance with this guide, this practice is also recommended in the Code Conventions for the Java Programming Language Conventions 2009 Section 6.1, "Number Per Line".

...

Code Block
bgColor#ccccff
int i = 1;  // purpose of i...
int j = 1;  // purpose of j...

Noncompliant Code Example (variables or fields and functions)

This noncompliant code example is easily misunderstood by a programmer.

Code Block
bgColor#FFcccc

long dbaddr, getDbaddr(); 

compliant Solution (variables or fields and functions)

The purpose of each identifier is readily apparent in this compliant solution.

Code Block
bgColor#FFcccc

long dbaddr;       // db address
long getDbaddr();  // function to access db address

Noncompliant Code Example (different types)

Wiki Markup
In this noncompliant code example, a programmer or code reviewer could mistakenly believe that the variables {{src}} and {{c}} are both declared to be type {{int}}. In fact, {{src}} is of type {{int\[\]}}, while {{c}} has a type of {{int}}.

Code Block
bgColor#FFcccc

int src[], c;

Note: this example declares the array in an antiquated and unpopular style, with the brackets appearing after the variable name. Arrays should be declared type[] name for improved clarity.

Compliant Solution (different types)

In this compliant solution, each variable is declared on a separate line, using the preferred style for declaring arrays.

Code Block
bgColor#ccccff

int[] src;   /* source array */
int c;       /* max value    */

Although this change has no effect on compilation, it clarifies the programmer's intent.

...

Compliant Solution (initialization)

In this compliant solution, it is readily apparent that both i and j are initialized to 1.

Code Block
bgColor#ccccff

int i = 1, j = 1;

It is preferrable to declare each variable on a separate line, but multiple variables on one line can be acceptable if they are small temporary variables, such as array indexes.

Noncompliant Code Example (different types)

In this noncompliant code example, the programmer declared declares multiple variables, including an array, on the same line. All instances of the type T have access to methods of the class Object. However, it is easy to forget that arrays require special treatment when some of these methods are overridden.

Code Block
bgColor#FFcccc
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 method of Object such as toString() is overridden, a programmer might 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.

Code Block
// The oversight leads to an incorrect implementation
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;
}

Compliant Solution (different types)

This compliant solution places each declaration on its own line, as well as using the preferred notation for array declaration.

...