Are public members variables in Java 8 interfaces a feature or an implementation side-effect/defect?
This question pertains to the pre-release Java 8 build lambda-8-b50-linux-x64-26_jul_2012.tar.gz.
Java 8 introduces new features to interfaces in the form of default methods. Casual testing with the JDK8 lambda compiler allows interfaces of this form:
public interface Foo {
public int foo = 0;
int foo() default { return foo; }
}
Sample implementing type:
public class FooImpl implements Foo {
public int foo = 1;
}
This code follows the standard conventions for variable shadowing:
Foo f = new FooImpl();
System.out.println(f.foo());
System.out.println(f.foo);
System.out.println(new FooImpl().foo);
Output:
0
0
1
The documentation (JSR 335: Lambda Expressions for the Java™ Programming Language Version 0.5.1) doesn’t mention member variables. I’m inclined to think the compiler is being too tolerant but perhaps I’ve missed something.
Public fields in interfaces are not a new features in Java 8. If you remember that they are implicitly static and final, the results you are seeing make perfect sense.