Object Declaration
What is difference when we declare ....
Dog d = new Dog();
and...
Dog d =null;
d = xyz();
What is difference when we declare ....
Dog d = new Dog();
and...
Dog d =null;
d = xyz();
Depends on what is returned by xyz(),
if xyz() returns a Dog object, then the two statements below will have the same results.
Otherwise, if xyz() returns a subclass of Dog (e.g. Bulldog), then the results for the two statements below may be different due to polymorphism. Pls use google to find more about polymorphism.
> What is difference when we declare ....
Can't you see?
> Dog d = new Dog();
>
> and...
>
> Dog d =null;
>
> d = xyz();
The former statement is creating a new instance and assigning it to a variable d of type Dog. The latter is declaring a variable d of type null and assigning a reference of the Dog instance returned by a method xyz().