Robert

63%
Flag icon
Nearly every local variable declaration should contain an initializer. If you don’t yet have enough information to initialize a variable sensibly, you should postpone the declaration until you do. One exception to this rule concerns try-catch statements. If a variable is initialized to an expression whose evaluation can throw a checked exception, the variable must be initialized inside a try block (unless the enclosing method can propagate the exception).
Robert
I would take this exception further: For variables that are initialized inside of a try-catch (or other control block, like if/elseif/else), do _not_ also initialize the variable at the time of declaration. •This makes it clear (to someone else reading the code) that the variable only gets a usable value by making it through the block. • It's also a useful practice while writing the block; it helps ensure every path that should initialize it does • It makes it clearer what paths result in that "default" value (rather than having to determine/infer from the paths that don't set it)
Effective Java
Rate this book
Clear rating
Open Preview