This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes question about null object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question about null object" Watch "question about null object" New topic
Author

question about null object

Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
Hi,

It may sound stupid question, but I must ask
In the code below the object reference d2 is cerated using null d1. so what this object is like? is it also null?
also with line Dog d1 = null, do we get an object on the heap?



Thanks in advance.

Anu


SCJP5.0, SCJA
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38472
    
  23
Have you tried it? Will that code actually compile?
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46


you are using the non default Dog Constructor ..where is the implementaion of it ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@jittu goud: One way folks try to help beginners is to get them to think things through for themselves rather than just handing the answer to them!
Anu Bhagat
Ranch Hand

Joined: Jun 20, 2008
Posts: 64
the complete code is like this


My question is- is d2 null? ( i think it should be - but not sure )do we get any object on heap? do null onject live on the heap?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why do you think it would be null? What, precisely, is happening when you execute the Dog(Dog d) constructor? Is is still instantiating a Dog?
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
David Newton wrote:@jittu goud: One way folks try to help beginners is to get them to think things through for themselves rather than just handing the answer to them!


oops my bad ....on the other side...iam a beginner myself..... :shock:
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46
anu Its not null...

Dog d2 = new Dog(d1) ...

i think the "new Dog(d1)" will create a new object with null value
...but d2 will store the object reference ..rather than the actual object

in this case you should get the value of d2 as Dog@3e25a5

but still the d2.d will be null
jittu goud
Ranch Hand

Joined: Mar 30, 2007
Posts: 46

i made a few changes....to your code
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: question about null object