Member initialization

Java goes out of its way to guarantee that variables are properly initialized before they are used. In the case of variables that are defined locally to a method, this guarantee comes in the form of a compile-time error. So if you say:

void f() {
int i;
i++; // Error -- i not initialized
}

youll get an error message that says that i might not have been initialized. Of course, the compiler could have given i a default value, but its more likely that this is a programmer error and a default value would have covered that up. Forcing the programmer to provide an initialization value is more likely to catch a bug.

If a primitive is a field in a class, however, things are a bit different. Since any method can initialize or use that data, it might not be practical to force the user to initialize it to its appropriate value before the data is used. However, its unsafe to leave it with a garbage value, so each primitive field of a class is guaranteed to get an initial value. Those values can be seen here:

You can see that even though the values are not specified, they automatically get initialized (The char value is a zero, which prints as a space). So at least theres no threat of working with uninitialized variables.

Youll see later that when you define an object reference inside a class without initializing it to a new object, that reference is given a special value of null (which is a Java keyword).