Every type in Java has a primitive value when declared. The article Primitive Data Types contains a description for primitive data types. Knowing this, why does Eclipse show an error telling me the variable may not have been initialized?
If I have, for example,
int x;
x++;
From the reference:
Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. If you cannot initialize your local variable where it is declared, make sure to assign it a value before you attempt to use it. Accessing an uninitialized local variable will result in a compile-time error.
From the Java Language Specification, Java SE 8 Edition, 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 using the rules for definite assignment (§16 (Definite Assignment)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With