Instance data member vs Local variables (primitive/object reference)
just a simple question: why does a local variable should be initialized before a program's running stage? considering that this variable will be used somewhere local, while a data member can be used even if it is not initialized in a constructor or an instance method.
does the compiler always assign an offset values in data members when they are not initialized explicitly?
Re: Instance data member vs Local variables (primitive/object reference)
Here is an explanation for both of your questions: oop - Uninitialized variables and members in Java - Stack Overflow
The compiler forces you to initialize local variables partly because it can not figure out the full logic of your program flow.
Edit: My compiler optimizes away the first increment but not the second. Whether this should be an error or a warning is
debatable but that is the way it is in java.
Code java:
public void foo() {
int x;
final int y = 0;
if(false)
x++;
if(y == 0)
x++;
}