This is explained rather well by the Java Language Specification (specifically, §4.12.5):
Every variable in a program must have a value before its value is used:
- Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):
- For all reference types (§4.3), the default value is null.
- A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment (§16).
To expand a bit, §16 goes into the rules for definite assignment, which is at the crux of the reason:
Each local variable (§14.4) and every blank final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs.
For every access of a local variable or blank final field x, x must be definitely assigned before the access, or a compile-time error occurs.
Simply put: Java will assign default values to class/instance variables, but will not assign a default value to a local variable. Local variables must be definitely assigned in this manner (either by initialization or assignment), or a compile time error will occur (as you observe).
If you think of it from another angle, when you initialize a class that contains specific fields, you may not want those initialized to a value at first (think JavaBeans). If you're in a code block and you declare a variable, the expectation instead is on the developer to control that object's life cycle right there in the block.
It doesn't make sense to simply declare a variable and attempt to do something with it without assigning a value to it, as the variable doesn't have a value.