File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question #8 from Doug's book

 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anupreet Arora
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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!
Regards,
Anupreet
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards
Maulin
 
Alton Hernandez
Ranch Hand
Posts: 443
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I liked this one
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic