• 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

extending Inner classes

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am extending inner class in my inner class but my enclosing class of the inner subclass is not a subclass of Outer but i am passing explicit reference to an object of Outer to a constructor of Inner. Problem is my member variables of inner subclass are not getting initialized nor initialization block is executed even though object of inner class is created successfully
Can any one tell me what is wrong in this code

I am expecting out put like
I am called
z = 10
but i am getting
z = 0
only

Edited by Cindy Glass to format Code.

[This message has been edited by Cindy Glass (edited January 23, 2001).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your example! Its working as expected!
BTW I dont think you can have your inner class as public!
-Ciao
------------------
Empowering the internet Generation!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this is a tough one. I haven't found anything in the Java Language Specification that directly explains this situation.
The one thing I did find was a referende to the purpose of initializer blocks:

The Java language supports instance initialization blocks, which you could use instead (of constructors). However, these are intended to be used with anonymous classes, which cannot declare constructors.


Of course the solution is to put the initialization in the constructor as SUN intended. However the answer to why the initializer of the inner class which is a sub-class of an inner class wiht a different super class did not get invoked is still under investigation.
I played with your question this way:

And I got the following result:
I am in Eouter initializer block
I am in Eouter constructor
I am in Outer initializer block
I am in Outer constructor
I am in Inner initializer block
I am in Inner constructor
I am in Iinner constructor
z=0
x=10

 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's move this to advanced and see if the guru's over there can explain why the initializer was not invoked.
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My suggestion: Please do not do this!!!
Read: Can inner class extends outer class, or opposite? Why we should generally avoid using this technique?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True - but the specification says that the initializers WILL be invoked before the constructor - and it wasn't.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDK 1.2.2 has an assortment of bugs when dealing with the more convoluted uses of inner classes. This is evidently one of them. JDK 1.3 compiles and runs the code according to spec.
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic