0

I'm trying to understand variable declarations in Java.

Actually, I'm trying to understand why you would not declare variables at the top of class, initiate them, and then use as needed.

In the Headstart Java book it states local variables are declared within a method and initiated within the code block. I understand that aspect.

My question is, if you know the variable, why would you not declare it at the beggining of the method, instead of waiting and declaring/initializing in the middle of code?

Wouldn't that make code harder to read?

ex:

Class Foo {

 while.... blah blah

   int bar = 3 + dog.getsize();

}
Not a bug
  • 4,286
  • 2
  • 40
  • 80
rathor1622
  • 23
  • 4
  • 1
    possible duplicate [Declare local variables as late as possible](http://stackoverflow.com/questions/10204828/declare-local-variables-as-late-as-possible-or-at-the-nearest-curly-brace-they-b) – Reimeus Jan 30 '14 at 18:38

4 Answers4

2

This is really a matter of style. However, it is generally best practice to declare local variables near to when they are first used. It makes code reading simpler to comprehend. Imagine a large method with hundreds of lines (bad practice by the way :)). If a variable is declared near the top then referenced hundreds of lines later, it can make code reading difficult.

cmd
  • 11,622
  • 7
  • 51
  • 61
  • I see. My thought process was thinking having it upfront, but as you say, by the time i get to the implementation of whatever code it is, I would have to just back track, find that variable and then use it. Makes sense now. Thank you! – rathor1622 Jan 30 '14 at 18:51
1

If a method has many variables, you could end up with a "top heavy" method with many declarations before any of the real code. Also, if your method is long, you may have to search for the declaration of a variable to find out its type.

Also, variables can be initialized when they are declared, which makes for shorter code. By putting the declaration close to where it is used, it is both relevant to the section of the method, and easy to find the type. It is both easier to read, and write such code.

That being said, I prefer to put most of my variable declarations (and initialization) at the top of a block or method to which they are relevant.

I only put methods at the class level if they need to persist between method calls. Anything that is only relevant to the method should be declared in the method. Things relevant to a code block are declared at the top of the block.

Trenin
  • 2,041
  • 1
  • 14
  • 20
0

Assume that your method is 50 lines long, and you only need to use this variable on line 48. By the time you have read the first 47 lines, you may have trouble remembering how that variable is spelled and what its data type is. That's why it's a good idea for local variables to be declared shortly before they will be needed. There is an exception if the variable would be declared inside a try block and you need to check the value after the try block finishes, but in that case you declare right before the try.

Teresa Carrigan
  • 668
  • 8
  • 14
0

From JavaDoc

In java there are four kind of variable :

  1. Instance Variables (Non-Static Fields)
  2. Class Variables (Static Fields)
  3. Local Variables
  4. Parameters

If you are declaring a variable in a class, than java will initialize its value for you. while declaring a local variable you have to initialize its value to avoid Variable might not have been initialized error

Although it will always depend upon scope of variable and your algorithm. you should always care about readability of your code while declaring.

Further reference on Initialization in java.

Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80