aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarification on Q6 Self-Test Chapter 3 K&B SCJP5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarification on Q6 Self-Test Chapter 3 K&B SCJP5" Watch "Clarification on Q6 Self-Test Chapter 3 K&B SCJP5" New topic
Author

Clarification on Q6 Self-Test Chapter 3 K&B SCJP5

meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75


The answer is: hi hi followed by an exception.
I am not clear on how this is so.
After line 2 are both m2 and m1 pointing to the same object?
line 3 is executed and prints "hi".
line 4 assigns m1 to m4. At this point m2, m1 and m4 are pointing to the same object on the heap. correct?
line 5 is executed and prints "hi".
line 6 assigns m1 to m5 . At this point m2, m1, m4 and m5 are pointing to the same object.
Then line 7 is executed. Is at this point that an exception is thrown?
The explanation given by K&B merely mentions that "m2 object's m1 instance variable is never initialized so when m5 tries to use it a NullPointerException is thrown."
In that case why did we not get a NullPointerException at
Line 4 Mixer m4 = m3.m1;
itself because m1 is null here as well?
Thanks,
Meera


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
meera kanekal wrote:

In that case why did we not get a NullPointerException at
Line 4 Mixer m4 = m3.m1;
itself because m1 is null here as well?

Thanks,
Meera

Hi Meera,

The reason why you don't get an exception at line 4 is because m3.m1 is not null, since the object m3 points to was instantiated (at line 2) using the constructor that takes a Mixer object, with m2 as the argument. After line 2, m3.m1 points to the same object as m2.
But when the object that m2 points to was instantiated (line 1) the no-arg constructor was used, so that left m2.m1 with a default value of null.


All code in my posts, unless a source is explicitly mentioned, is my own.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
line 4 should read "Mixer m1;"


SCJP 88% | SCWCD 84%
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
Thanks for your reply Ruben. This question is clear for me now.
Meera
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Excellent, Meera!
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Sorry for barging in here, but whilst reproducing this in Eclipse, I get a compiler error from:



In my opinion, quite rightly so, where is this m1 variable being declared and then called from, on m3??

To give the benefit of the doubt for a possible typo, on the instance variable for the Mixer class did you intend to write:



instead of



if you did then, it compiles: Plese use the preview function to check mistakes before posting or use edit to correct once posted (like i did )




Looking at the code, and running it I get the nullPointerException where m5.go() is called. m5 is pointing to m2.m1. However m2 was created with a no-arg constructor and then used to construct m3. So providing my assumption about m being m1 was correct, there is no instance of m2.m1 to call go on, and hence the nullPointerReference.

?


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
meera kanekal
Ranch Hand

Joined: Feb 13, 2005
Posts: 75
Corrected Code
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Yup thats what I thought, I also had a look at the question myself
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification on Q6 Self-Test Chapter 3 K&B SCJP5