• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaration of variables / references - What difference does it make?  RSS feed

 
Srinath Kannan
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I wouldn't be worrying much about the compiler or JVM. I'd use the first approach because I think it's far easier to read.
 
Srinath Kannan
Greenhorn
Posts: 14
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew,

Thanks your comments!

All these days i was using option 2, from now i will use option 1. Other than readability, like in terms of memory allocation / performance do you see any difference? Is that going for option 1 guarantee's correct de-allocation at the right time!
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My preference is to minimize variable scope; declare them
at first use and then expand their scope only as necessary.
This provides minimum typing and maximum safety.

Jim ...
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinath Kannan wrote:Hi Matthew,

Thanks your comments!

All these days i was using option 2, from now i will use option 1. Other than readability, like in terms of memory allocation / performance do you see any difference? Is that going for option 1 guarantee's correct de-allocation at the right time!


There is not enough difference in memory allocation / performance for you to alter your code at all. Readability, making it easier to change the code should it need it, etc., should be your primary considerations.

rc
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!