Question: There seems to be two accepted variable declaration placements for Java variables, each with different *raison d'être*.

From the [Sun's code conventions](http://java.sun.com/docs/codeconv/html/CodeConventions.doc5.html) we can see:

> 
> 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.
> 
> 
> 

However in the highly praised ["Code Complete"](https://rads.stackoverflow.com/amzn/click/com/0735619670) and some other authors advocate for reducing the scope of a variable to a minimum. That is basically waiting to declare variables until their first use.

These two approaches are clearly contradictory although I can see the point for both of them.

Which should I follow? Is there any consensus on the matter?

Answer: 
Variables *should* be declared as close to their use as possible, for two reasons: 

* Vertical locality
* Tool hinting

**Vertical locality** makes it easier to reason about chunks of code. The less scanning a reader has to do the easier it is to understand what code does, and what side-effects it 

Reducing variable scope also allows better **tool hinting** for automated tools, like refactoring. The closer together related things are the more obvious they are.

That said: if a method is long enough that the above points come in to play, it's likely the method is already too long, and should be refactored anyway.
I also recommend "Effective Java" by Joshua Bloch for more reasons why one should declare variables where they are first used.