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 ?
5 Answers
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":
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 by the compiler using the rules for definite assignment (§16).

- 84,978
- 11
- 107
- 151
The Local variables are stored on the stack and aren't actually created until they are initialized. If a local variable isn't used, it doesn't go on the stack. So if you want to use any Local variable you are supposed to initialize it.
However, Instance variables are allocated in the heap, and thus have a default placeholder.
All Instance variables have some default value if not initialized manually.
-
_So if you want to use any Local variable you are supposed to define it_. Define or initialize ? – Santosh Nov 29 '11 at 14:01
-
well, by defining (**not Declaring) I meant initializing. Because that is when your variable will be actually created. I updated my answer by the way. – gprathour Nov 29 '11 at 14:05
Why would you want such behaviour? If you're declaring a variable for local use, wouldn't you want to initialise it as soon as possible to its required initial value? Instance variables are initialised to a default value on instance initialisation because they have a lifetime greater than the scope of declaration and initialisation.

- 1,884
- 2
- 25
- 36
-
My question was from other perspective. In C the declared variables always have some default value. So why not same in java ? Off course there must be reasons. That's what my question is about. – Santosh Nov 29 '11 at 13:59
It is not a stack/heap issue. The design of Java has several good & simple programming style items. You want an object to be initialized without any doubt. For local variables however the compiler can easily issue a warning when it might be used before initialisation. Furthermore the style declaration + initialization is desired:
int n = takeN();
Initialisation would require zeroing of the stack frame, which is an unnecessary overhead.

- 107,315
- 7
- 83
- 138
There are likely several reasons for this. A couple:
First, as previously noted, uninitialized variables are a major source of bugs, and, while it's impractical to check for uninitialized instance variables, it's quite easy for the compiler to check for and "diagnose" uninitialized local variables -- it actually falls out with little additional effort from the data flow analysis.
Second, initializing local variables takes time. Instance variables can be "initialized" by simply zeroing the instance, and it only needs to occur once per instance (and needs to be done for "security" and for GC purposes anyway), but local variables are allocated here and there -- some in stack, some in registers, and clearing them takes real cycles every time the method's invoked.

- 47,103
- 17
- 93
- 151