Hi Michael,
I suppose the other folks that didn't respond were, rightly, concerned that the
question is not a Linux-Java issue but I will try to addess it anyway since you
brought it back up.
Your example, if I remember right from when I took the Java Certification test
last year, is a varient of a question on the test. If you built a slightly more
extended test you would have noticed that your call to "callFooBar()" in Parent2
from Parent1 occured before Parent2 was initialized. This means you were trying
to talk to "InnerOuter.this" before the inner class had an initialized pointer to
it.
This isn't a bug but part of the language spec. Since it will always be there one
has to plan for constructor issues like it.
I suspect that language design issues fit better in Java programmer groups.
Have fun,
Rick Kiss
Slightly more informative example.
----------------------------------------------------------------
class Parent1 {
public Parent1(){
callFooBar();
}
public void callFooBar(){
System.out.println(" Parent1 callFoobar");
}
}
public class InnerOuter {
// Inner class
class Parent2 extends Parent1 {
public Parent2() {
callFooBar();
}
public void callFooBar(){
System.out.println("Parent2 callFooBar - - - " + this);
try {
System.out.println(" Parent2 Foobar " + InnerOuter.this);
} catch (Exception e) {
System.out.println(" Parent2 Foobar " + e);
}
if (InnerOuter.this == null)
System.out.println(" InnerOuter.this == null - class not
initialized");
else
testOut(this);
}
}
public InnerOuter() {
testOut(this);
Parent2 p2 = new Parent2();
}
public void testOut(Object ob) {
System.out.println("TEST OUT: "+ob);
}
public static void main( String[] args ) {
InnerOuter test1 = new InnerOuter();
}
}
----------------------------------------------------------------
Michael Emmel wrote:
> 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]
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]