• 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

Question #8 from Doug's book

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I liked this one
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic