Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Q: Anonymous inner class extending outer class

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

I just finished K&B chapter on inner classes. I was happily experimenting around by writing small test programs till I got stuck with this one.


I don't understand why making outerClass.new InnerClass() {}; causes compile time error. The error message says something like "No enclosing instance of type OuterClass is available due to some intermediate constructor invocation". Can anyone please explain me this? Thanks
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you know that you need an instance of OuterClass to instantiate InnerClass (as it is a non-static inner class). In the same way, to extend InnerClass, you need an instance of OuterClass to call the Constructor of InnerClass. Basically if I want to extend InnerClass, I would have to write a code like this
But since anonymous inner classes can't have constructors, so you can't create an anonymous inner class of a non-static inner class...
 
Innar Made
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ankit,

Thanks for your reply. Just to confirm that I understood you correctly:
1. anonymous inner classes cannot define new constructors;
2. if inner class InnerClass extends outer class OuterClass then subclassing InnerClass by making an anonymous inner class fails because it cannot call the parent constructor of InnerClass as InnerClass doesn't have instance of OuterClass.
 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Anonymous inner classes can't have any constructor
2. It doesn't matter whether InnerClass extends OuterClass or not, you can't create an anonymous inner class of a non-static inner class...
 
Innar Made
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey again, Ankit : )

Can you elaborate more about your statement?


2. It doesn't matter whether InnerClass extends OuterClass or not, you can't create an anonymous inner class of a non-static inner class...



Here I am making an anonymous inner class from a non-static inner class, it compiles and prints out "Greetings from inner class"


Thanks for your patience.
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting discussion!

It seems that in your first code the Inner Class extends Outer Class. And you CANNOT create an "anonymous inner class" out of the "Inner Class".

In second case, he Inner Class does NOT extend the Outer Class. And you CAN create an "anonymous inner class" out of the "Inner Class".

 
Ankit Garg
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which version of JDK are you using?? Are you using Eclipse?? Your original code compiles fine on my machine (JDK 1.6 build 11). And what I wrote about creating anonymous inner class of non-static inner class was wrong (I need to brush up my inner classes again )...
 
Innar Made
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

I am using JDK 1.6.0.18 with Eclipse Galileo. You are right, Ankit, compiling from command line it all works! I guess the problem is solved then
 
Goodbye moon men. Hello tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic