Raj Kamal wrote:Having variables declared at the head of a function makes the code more readable, for one.
Says who? Depends on what you are used to, for one. I, for one, find the other (only when needed) much more readable; all variables in the header remind me of the old-school coding, like in Pascal.
Raj Kamal wrote:whereas if each is declared in the appropriate block as per the necessity makes them eligible for garbage collection earlier before the method has finished execution
How is this possible? They will be visible from the point they are declared until the end of the block, which in the case described here is method block. So now, they cannot be collected earlier, unless you explicitly assign null to the references or do this dreadful thing:
The only concern could be that the objects that are allocated at the top of the method will not be necessary (for example, there is an if and they will be used only if something happens, or there is an exception). But, do you really care about this - the compilers are currently very very smart, they will optimize the code anyways.