Bug accessing variable in superclass from inner class of subclass.

Mark J. Roberts mjr@statesmean.com
Thu Feb 22 08:28:00 GMT 2001


I haven't seen this in GNATS, so I'll report it once again, as it seems
my post in mid-January was overlooked (or perhaps the problem is simply
difficult to solve). I apologize if this is already known.

This problem does not occur of Super and Inner are not inner classes of
Test.

// When id is a member of Inner's superclass, and Inner2 tries to access
// it, it is munged to 0.

public class Test {
    public static void main(String[] args) {
        Test t = new Test();
    }

    public Test() {
        (new Inner()).doit();
    }

    public class Super {
        public int id = 42;
    }

    public class Inner extends Super {
        public void doit() {
            System.out.println("works here: "+id);
            (new Inner2()).doit();
        }

        public class Inner2 {
            public void doit() {
                System.out.println("but not here: "+id);
            }
        }
    }
}

mjr::mjr$ javac Test.java ; java -cp . Test
works here: 42
but not here: 42
mjr::mjr$ gcj --main=Test -o test Test.java ; ./test
works here: 42
but not here: 0
mjr::mjr$


-- 
Mark Roberts
mjr@statesmean.com




More information about the Java mailing list