Let's assume we have this example code:
Fish fish;
fish.equals(null);
fish.equals(fish);
The equals method has not been overwritten and the fish variable has not been initialized which means, that java will nullify it?
So would both line return true
?
And why would that be so? Would it also be possible to use an class variable when the object is not initialized?
If
fish
is a local variable, this code won't pass compilation, sincefish
must be initialized.If
fish
is an instance variable, this code will throw NullPointerException, since the default value offish
will be null, and de-referencing a null reference throws this exception.