PDOBB - Put Declarations Only at the Beginning of Blocks

Sun Code Conventions for Java recommends to put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope.

Wrong

void myMethod() {
    if (condition) {
        doSomeWork();
        int int2 = 0;
        useInt2(int2);
    }
    int int1 = 0;
    useInt1(int1);
}

Tip: Move declarations to the beginning of the block.

Right

void myMethod() {
    int int1 = 0; // beginning of method block
    if (condition) {
        int int2 = 0; // beginning of "if" block
        doSomeWork();
        useInt2(int2);
    }
    useInt1(int1); }