• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Explanation for output

 
Ranch Hand
Posts: 66
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please UseCodeTags. You can edit your post with the button.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ 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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Which part?
 
archu sweet
Ranch Hand
Posts: 66
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@ Matthew Brown:


Mixer m4 = m3.m1;


iam unable to understand this statement.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Matthew Brown :

That was fantastic...u made it so simpler....Thanks a ton!!
 
Don't MAKE me come back there with this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic