In Java, should variables be declared at the top of a function, or as they’re needed?

Declare variables as close to the first spot that you use them as possible. It’s not really anything to do with efficiency, but makes your code much more readable. The closer a variable is declared to where it is used, the less scrolling/searching you have to do when reading the code later. Declaring variables closer to the first spot they’re used will also naturally narrow their scope.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)