Object Declaration

What is difference when we declare ....

Dog d = new Dog();

and...

Dog d =null;

d = xyz();

[141 byte] By [prady_joa] at [2007-11-27 10:47:42]
# 1

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.

redfragrancea at 2007-7-28 22:02:31 > top of Java-index,Java Essentials,Java Programming...
# 2

> 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().

aniseeda at 2007-7-28 22:02:31 > top of Java-index,Java Essentials,Java Programming...