I understand that local variables are allocated on stack where as member variable or instance variables are allocated on heap. But I am curious about why the JVM cannot assign a default value to a local variable on stack if its not assigned any value (while invoking that method)? Has this anything to do with the way a compiler/JVM/Processor works ?
I understand that local variables are allocated on stack where as member variable or
Share
It could. However, one of the most frequent errors is unintentionally uninitialized local variables. Making those illegal by definition allows such errors to be caught with static analysis.
From JLS 3, in section “4.12.5 Initial Values of Variables”: