To extend my own question I think this is a bug since thre is no way
for a programmer to intialize the enclsing instance variable.
I think that Object should have a methos added.

getEnclosingInstance()


Then you coud do ..

       public void callFooBar(){
            if( OuterClass.this == null ) {
                    OuterClass.this = getEnclosingInstance();
            }
            System.out.println(" Parent2 Foobar " +OuterClass.this);
        }

My point being that the subclass should alwas be able to access its enclosing
class.
The probelm is thre is no way for a prgrammer to init the instance variable
that I know of.

This does not work either


class Parent2 extends Parent1 {
            Parent2( OuterClass outer ) {
                outer.super();
            }
           public void callFooBar(){
               System.out.println(" Parent2 Foobar " +OuterClass.this);
           }

       }


Anyway I think it is a bug.


Michael Emmel wrote:

> This generates the following output ..
>
> Parent2 Foobar null
>
> I understand why but  it  compiles and fails at runtime.
> Thus if you subclass a class with and inner class any method used is the
> parents consturctor cannont
> acces the Outer class.
>
> Of course I may be doing something stupid but I know the  instance.super
> stuff and this is the referse case.
> It seems a bit pathalogical and also it compiles fine even though it
> will never run.
> The null pointer excetion if you access  OuterClass.this is not exactly
> helpful either.
>
> Any thoughts ??
>
> Mike
>
> class Parent1 {
>
>     public Parent1(){
>         callFooBar();
>     }
>
>     public void callFooBar(){
>         System.out.println(" Parent1 Foobar");
>     }
>
> }
>
> public class OuterClass {
>
>     class Parent2 extends Parent1 {
>
>         public void callFooBar(){
>             System.out.println(" Parent2 Foobar " +OuterClass.this);
>         }
>
>     }
>
>     public OuterClass() {
>
>         Parent2 p2 = new Parent2();
>     }
>
>     public static void main( String[] args ) {
>             OuterClass test1 = new OuterClass();
>
>     }
>
> }
>
> ----------------------------------------------------------------------
> To UNSUBSCRIBE, email to [EMAIL PROTECTED]
> with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to