Why data members are initialized with default values
When you compile a class, the Java compiler creates an instance initialization method for each constructor you declare in the source code of the class. Although the constructor is not a method, the instance initialization method is. It has a name, , a return type, void, and a set of parameters that match the parameters of the constructor from which it was generated.
If you don't explicitly declare a constructor in a class, the Java compiler will create a default constructor on the fly, then translate that default constructor into a corresponding instance initialization method. Thus, every class will have at least one instance initialization method.
Whereas local variables are not
When you allocate resources for a local variable, Java doesn't write a value into the memory. The reason you get an error is because Java makes sure you give it a value before you use it.
The stack contains the local variables, so they might take the value of whatever was on the stack before them. To guard against this, the compiler can check whether they have been initialised or not.