my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Explanation for output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Explanation for output" Watch "Explanation for output" New topic
Author

Explanation for output

archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66


What is the result?
A. hi
B. hi hi
C. hi hi hi
D. Compilation fails
E. hi, followed by an exception
F. hi hi, followed by an exception
Answer:
F is correct. The m2 object’s m1 instance variable is never initialized, so when m5 tries to
use it a NullPointerException is thrown.


Please explain me the output...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please QuoteYourSources.


[My Blog]
All roads lead to JavaRanch
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And please UseCodeTags. You can edit your post with the button.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

The answer given explains why the output is as it is. m2 is created as a new Mixer, and its instance variable m2.m1 is never initialised. Therefore it's null. So m5 is set to null. So m5.go() throws a NullPointerException. Is there a particular part of that you're having trouble with?
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

@ Wouter Oet : yeah sure wll do it.....

@ Christophe Verré : i got it from Kathy sieera scjp 1.5 edition

@ Matthew Brown : yes this part is troubling me....
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

archu sweet wrote:@ Matthew Brown : yes this part is troubling me....

Which part?
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66


@ Matthew Brown:


Mixer m4 = m3.m1;


iam unable to understand this statement.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

OK, I'll have a go in more detail...

The class Mixer has a member variable m1, that also can point to a Mixer object (so a Mixer 'has-a' Mixer). So whenever we have a Mixer m, we can refer to m.m1.

However, there are two ways of creating a Mixer. One constructor takes a Mixer as an argument, and initialises m1 to that value. That's what's happening on line 8 (I've just sorted out the code tags for you to make it easier to read). The other constructor takes no arguments, and if we use that m1 is initialised to null. That's what happens on line 7.

So, after line 7, m2 points to a Mixer object, but m2.m1 is null.
After line 8, m3 points to a Mixer object, but m3.m1 references the same object as m2.

On line 10, we set m4 to the value of m3.m1, which is (see line above) the same object as m2. Calling m4.go() is the same as calling m2.go().

On line 12, we set m5 to the value of m2.m1. Which (see above again) is null. So after line 12, m5 is null. Which is why we get an exception on line 13.

Does that help?
archu sweet
Ranch Hand

Joined: Mar 07, 2011
Posts: 66

@ Matthew Brown :

That was fantastic...u made it so simpler....Thanks a ton!!
 
 
subject: Explanation for output
 
Similar Threads
explain output
Q4,Self Test, Chapter 3 (K & B)
assignment
initialization
output not clear