Talk:Initializing local variables in Java

From Nokia Developer Wiki
Jump to: navigation, search

Saikrrishna.ch -

I more interested to know, why the Java compiler is not allowing to read the un initialized local variable? Where as it allows to read the un initialized instance variable by implicitly initializing to a default value.

Good. I din't know that, even NOKIA has the developer community like IBM etc...

saikrrishna.ch 07:02, 22 March 2012 (EET)

Grahamhughes -

It does not allow access to uninitialized local variables, because it is a rule of the Java language.


Because instance variables are automatically initialized, but local variables are not. If you read them, you would (as in a C program) read some random piece of information depending on what the memory was used for previously.

Why not auto-initialize locals?

Because it takes time. It would add significantly to the overhead of invoking each method, making programs slower.

grahamhughes 15:24, 9 February 2013 (EET)