If implicit constructors automatically initializes all variables to their default values , then why does Java gives compile time errors like " reference variable not initialized" ?
-
1Are you sure this error is about field and not some local variable? Can you post some code example with this error?Pshemo– Pshemo2014-07-07 20:06:39 +00:00Commented Jul 7, 2014 at 20:06
-
Implicit/default constructors do not initialize variables to their default values. To get an answer for why you are getting the "reference variable not initialized" message, you need to provide more context.Nivas– Nivas2014-07-07 20:06:47 +00:00Commented Jul 7, 2014 at 20:06
-
possible duplicate of Uninitialized variables and members in JavaDomi– Domi2014-07-07 20:07:08 +00:00Commented Jul 7, 2014 at 20:07
-
3That error is for local variables, not fields. The compiler checks local variables more thoroughly as they are easier to reason about as they are limited to one method.Peter Lawrey– Peter Lawrey2014-07-07 20:10:40 +00:00Commented Jul 7, 2014 at 20:10
Add a comment
|
2 Answers
All reference types in a class will be initialized as null if you do not set them to something sensible. I'm not aware of compiler errors for that case.
1 Comment
kajacx
I agree, I'm using netbeans 8 and it is capable of detecing a dereferencing of null-pointer (in some cases), but it isn't a compile error.