I find Java generics extremely confusing and if you ask me, their a
gigantic hack. One thing in the long list of baffling things is the
type of error you are seeing here...
The main difference in the two examples is in the second
AttributeBinding is a parameterized type. When you use the
AttributeBinding type in the main method you do not provide
parameters, so in the second example it is an non-generified
declaration where as in the first example it is actually a properly
generified type (since it has a no parameters, and yes this is super
confusing).
As you pointed out in the followup email, once you provide a type of
the parameter the compiler starts working as expected. It seems that
the compiler makes a simple decision that a type is properly
gentrified or not, and if it is not generified, it ignores all generic
declarations on the class (even the staticly declared ones like you
have here).
I avoid wildcard declarations <?> because the confuse the compiler
when passing wildcard generics to methods. Instead I use <Object>,
which is supposed to be the same thing as <?> but is isn't.
It is kind of depressing how poorly generics work in Java.
-dain
On Jul 1, 2008, at 3:58 PM, David Blevins wrote:
Anyone know why this works:
public class Test {
public interface AttributeBinding {
public Object getAttribute();
public List<Method> getMethod();
}
public static void main(String[] args) throws Exception {
AttributeBinding binding = null;
for (Method method : binding.getMethod()) {
}
}
}
But this seems to somehow change the List<Method> to plain List and
generates a compile error:
public class Test {
public interface AttributeBinding<A> {
public A getAttribute();
public List<Method> getMethod();
}
public static void main(String[] args) throws Exception {
AttributeBinding binding = null;
for (Method method : binding.getMethod()) {
}
}
}
Would love to know what the issue is.
-David