I have this code:
MyClass object;
.... some code here where object may or may not be initialised...
if (object.getId > 0) {
....
}
Which results in a compile error: object
may not have been initialised, which is fair enough.
Now I change my code to this:
MyClass object;
.... some conditional code here where object may or may not be initialised...
if (object != null && object.getId > 0) {
....
}
I get the same compile error! I have to initialise object
to null:
MyClass object = null;
So what's the difference between not initialising an object, and initialising to null? If I declare an object without initialisation isn't it null anyway?
Thanks