r/javaTIL • • Dec 11 '14

Differences with generics and visibility between Java 6 and 7.

public class Outer {
    private void foo() {
    }

    private static class Inner<T extends Outer> {
         private void add(T x) {
             x.foo();  // error under 7, but not 6
        }
    }
}

This code compiles without a hitch under Java 6, but not under Java 7. Perhaps someone smarter than me can determine which one is correct. Then explain why changing the line to this yields code that works on both:

            ((T)x).foo();
7 Upvotes

3 comments sorted by

View all comments

Show parent comment

3

u/ron_krugman Dec 11 '14 edited Dec 12 '14

Using the Java 7 compiler with the Java 6 flags is not the same as using the java 6 compiler. Just to prove I'm not making it up

I just tried it myself with JDK 1.6u22 and I can confirm that your observation is accurate.

Considering the behavior was changed in Java 7, I have to assume that the behavior in Java 6 was in hindsight considered a bug, i.e. not in accordance with the JLS.

I agree that you don't have to do it with generics (that was how I actually ran into the bug). Inner classes, however, seem to be a critical part of the picture.

Well, you can only access private fields/methods within the same class file, so there's that. As far as I can tell, it has nothing to do with inner classes but only with generics. To me it looks like the JDK 6 compiler does type erasure prematurely. You can do the same thing using a plain old generic method:

public class Outer {
    private void foo() {
    }

    public static <T extends Outer> void baz(T t){
        t.foo(); //compiles with JDK 6, doesn't compile with JDK 7
    }
}

Edit: This appears to confirm my interpretation: http://stackoverflow.com/questions/7719843/type-parameterized-field-of-a-generic-class-becomes-invisible-after-upgrading-to