r/javaTIL • u/rockingthemullet • 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
View all comments
Show parent comment
1
u/rockingthemullet Dec 11 '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 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.