views:

377

answers:

5

Why must local variables, including primitives, always be initialized in Java? Why is the same not applicable in the case of instance variables?

+13  A: 

There was a question about this very recently for C#... - read the answers there as well, as it's basically the same thing. You might also find Eric Lippert's recent blog post interesting; it's at least around the same area, even though it has a somewhat different thrust.

Basically, requiring a variable to be assigned a value before you read it is a Good Thing. It means you won't accidentally read something you didn't intend to. Yes, variables could have default values - but isn't it better for the compiler to be able to catch your bug instead, if it can prove that you're trying to read something which might not have been assigned yet? If you want to give a local variable a default value, you can always assign that explicitly.

Now that's fine for local variables - but for instance and static variables, the compiler has no way of knowing the order in which methods will be called. Will a property "setter" be called before the "getter"? It has no way of knowing, so it has no way of alerting you to the danger. That's why default values are used for instance/static variables - at least then you'll get a known value (0, false, null etc) instead of just "whatever happened to be in memory at the time." (It also removes the potential security issue of reading sensitive data which hadn't been explicitly wiped.)

Jon Skeet
AAhhh... you beat me by a few seconds :P
monksy
What I don't understand is, if a object is null by default, why do you have to assign it to null? I guess the Java language people decided it was better to be explicit in that case, but I'm not sure if I agree.
James McMahon
An object isn't null. A *variable* may be null, but not an object. And a local variable *doesn't* have a default value... precisely because not having one means you're forced to give it a useful value before it's read. If you want to make that value null, that's fine... but I rather like the compiler telling me if I'm trying to read something which might not have been initialized...
Jon Skeet
+1  A: 

Local variables and primitives should be initialized before use because you would know what to expect from the values. Historically, when a new variable was created it would contain random values from the memory [and one could not predict the value]. Java also requires this because it prevents the presence of orphaned variables.

monksy
A: 

In practice all variables need to be initialized before using them.

I can't think of a time you'd want to use a variable before setting its value (unless you're comparing it against null).

Tenner
+3  A: 

Well, in case of local variable it's clear what it means 'before' since the program flow between declaration (in method) and reference is sequential. In case of fields declared outside of the method compiler never knows which code is going to be used when so it cannot generate an error as possibly some other method is going to initialize the field before it is used.

quosoo
A: 

Not totally true. Local variables only need to be initialized if being reference. A local variable can be left uninitialized if never referenced. For example:

int x;  // Valid
int y;
println("y=" + y);  // Not valid since y's value has never been assigned
Steve Kuo