• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Q4,Self Test, Chapter 3 (K & B)

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers!

class Mixer{
Mixer(){ }
Mixer(Mixer m) { m1=m ;}
Mixer m1;

public static void main(String[] args){
Mixer m2= new Mixer();
Mixer m3= new Mixer(m2);
m3.go();
Mixer m4=m3.m1;
m4.go();
Mixer m5=m2.m1; m5.go();
}

void go()
{ System.out.print("hi");}
}

Output: hi hi, followed by an exception

Friends ,please help me in understanding the output.I couldn't.

Source: SCJP guide 6.0(K and B), page 270, Q4.
Thanks in advance
[ December 20, 2008: Message edited by: Raj kumar ]
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think the output should be?
 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi RajKumar,

Actually you will get compiler error since m1 is nonstatic you cannot access it from static context. Is this the correct code which given in the book? because i am wondering at the answers.

try out your code and check. if you make Maker m1 as static then output will be hi hi hi, not any exception.

Preetha
[ December 16, 2008: Message edited by: Preetha Arun ]
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj,

That is the correct answer (hi hi followed by an exception.) The issue here is that m1, as an instance variable of class Mixer, is initialized to null, and only when the object is constructed with the constructor that takes a Mixer argument it is initialized to point to an object.

There is a mistake in your listing. It should be:
Mixer m5=m2.m1; m5.go();

Since m2 was constructed via the default constructor, m2.m1 is null, and so is m5 after the assignment. Therefore, the m5.go() invocation causes a NPE.

Does that help?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all type your code correct. It is:


See at line 1

Mixer m5=m2.m1;

so m5=null why ?
as here m2.m1 is null why?
as Mixer m2= new Mixer(); it calls to Mixer(){} so m1 is not set for m2 object.

And calling m5.go() means null.go() will give you NullPointerException.
 
K Raj Kumar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for being delay and typing flaws in my code. I will make sure that it wont repeat.
@ Puneet
Thanks again, I have completely understood.
 
Space pants. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic