Hi,
Whenever i code in
Java, i had this habit of declaring variables, stacking them on top of method! In my perception i did this thinking that it improves clarity, readability.. also it was like a code beautification!
I am aware of the following
1 . Usually one would declare the variables/references in the top of method to decide its scoping..meaning they would require the variable or reference in the catch or finally block
2 . Declaring variables/references in the top of method and initializing them with Null or some values! would lead to Null exceptions if not instantiated inside subsequent parts of code
When i googled on this, It was said in some forum "that declare variable close to where you need it!" because its easy for JVM while doing deallocation!
Please see the below sample code snippet and help me to understand which is the best option in terms of coding standard / de-allocation?
Option 1:
Option 2:
Comparing option 1 and 2! what difference do you see in terms of coding standard / de-allocation?