File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question #8 from Doug's book 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 "Question #8 from Doug Watch "Question #8 from Doug New topic

Question #8 from Doug's book

Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What happens when you try to compile and run this code.
This one is a bit complex. Try to work it out. You may want to look at the decompiled ocde to figure out why what happens happens.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
This is quite similar in concept to the Question 6 you posted.
When the new InnerMemberSubclass object is created, there is a call to the base class constructor InnerMemberSuperclass().
This internally calls print(), which has been overridden in the subclass InnerMemberSubclass, and tries to print the value of s. But s has not been initialised yet. So it gives out a NullPointerException.
Please correct me if I am wrong somewhere!
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi Anupreet
Its not that reason. Because if 's' is not defined yet then it will have null and it wil print null in output.
I guess the output, run the code, found myself wrong. I'm trying to figureout why.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443

This code will cause a runtime error of "NullPointerException".
The problem is that the instance of the enclosing class Test is not yet set in the inner class when the call to the print() method was made.
When a call is made to the constructor of an inner class(InnerMemberSubclass), the instance of the enclosing class (Test) is passed on to it. This instance will be stored by the inner class as the variable this$0 to access the outerclass' members .
However, in this case, before this was all done, a call to the superclass' constructor was made (InnerMemberSuperclass) . The superclass calls print(), and since it is overridden, the InnerMemberSublcass' print() method is the one invoked. But at this point, this$0 has not yet been set, so that method cannot locate the member variable s thus causing the error.
[ August 20, 2003: Message edited by: Alton Hernandez ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Here is the compiler generated code:

As you can see, Alton is correct. The link variable is not set up until the constructor of Test$InnerMemberSubClass runs which occurs AFTER the constructor for the parent class. This is a product of the odd way that inner classes are actually handled. this$0 is a link variable that allows the inner class to communicate with the enclosing class. Until it is set, the inner class has no access to the enclosing class.
[ August 20, 2003: Message edited by: Thomas Paul ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I liked this one

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Question #8 from Doug's book
It's not a secret anymore!