15

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++;
1
  • 1
    only local variables are not initialized by default Commented Mar 13, 2012 at 16:15

6 Answers 6

20

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What is a "local variable"? Read the other answer.
9

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)).

Comments

7

Local variables don't get initialized.

This is a local variable:

void aaa() {
    int x;
}

This is an instance variable. These do get initialized automatically:

class X {
    int x;
}

6 Comments

@Mansuro: also for array contents.
static variables get intitialized also...As far as I know, it's like this: only local variables don't get intitialized. And array contents are automatically initialized.
if array contents are automatically initialized, why is there a NullPointerException when trying to access an element by index?
@Mansuro - That only happens with an array of reference types, because reference type variables default to null. Try instantiating an int[8] - each element will be 0.
an instance Object variable will also get initialized to null
|
3

Data Type Default Value (for fields)

byte 0

short 0

int 0

long 0L

float 0.0f

double 0.0d

char ‘u0000’

String (or any object) null

boolean false

4 Comments

But the question was: "Why does Eclipse show an error telling me the variable may not have been initialized?"
If a declared variable is a class member, they will get default values, however, local variables don't get default values, thus Eclipse shows that error @PeterMortensen
That ought to be in your answer.
Bland and de-formatted copy of docs.oracle.com/javase%2Ftutorial%2F/java/nutsandbolts/… without linking to it.
0

From the Primitive Data Types reference provided by you:

"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".

Comments

-4

What you see is not an error, but your Eclipse preference. You can change it to ignore uninitialized variables in Eclipse preference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.