Java generics compilation error - The method method(Class) in the type is not applicable for the arguments?

A little advice: when you are not sure why compiler prohibits some generic-related conversion, replace generic classes in question with List { private List l; public Bar(List l) { this. L = l; } public void operationOnBar(List arg) { l. AddAll(arg); } } List l1 = new ArrayList(); List l2 = l1; List l3 = Arrays.

AsList(new Type2()); new Foo(). Bar(l2). OperationOnBar(l3); Type1 t = l1.

Get(0); // Oops!

A little advice: when you are not sure why compiler prohibits some generic-related conversion, replace generic classes in question with List. Then it would be easy to find an example that breaks type safety. This replacement is correct since currently Java doesn't provide a way to conduct any a priory knowledge about possible behaviours of generic classes (i.e.It lacks a way to specify covariance and contravariance of generic classes in their declarations, as in C# 4 and Scala).

Therefore Class and List are equivalent for the compiler with respect to their possible behaviours, and compiler has to prohibit conversions that can cause problems with List for other generic classes as well. In your case: public class Bar { private List l; public Bar(List l) { this. L = l; } public void operationOnBar(List arg) { l.

AddAll(arg); } } List l1 = new ArrayList(); List l2 = l1; List l3 = Arrays. AsList(new Type2()); new Foo(). Bar(l2).

OperationOnBar(l3); Type1 t = l1. Get(0); // Oops!

You also can change the signature of the method operationOnBar to: public void operationOnBar(Class arg){.

You would agree that this shouldn't compile: 1 Class clazz = AnotherType. Class; 2 new Foo(). Bar(clazz).

OperationOnBar(Type. Class); The problem is javac is a little dumb; when compiling line#2, all it knows about variable clazz is its declared type; it forgets the concrete type it was assigned to. So what is assigned to clazz at line#1 doesn't matter, compiler must reject line#2.

We can imagine a smarter compiler that can track the concrete types, then your code can be compiled, as it is obviously safe and correct. Since that's not the case, sometimes programmers know more about types than the compiler, it is necessary that programmers do casts to convince the compiler.

The general way to deal with these sorts of problems is to introduce a generic argument for the repeated type, which generally means introducing a new generic method (a class would do as well, but isn't necessary). Public static void main(String args) { fn(Type. Class); } private static void fn(Class extendsInterfaceClazz) { new Foo().

Bar(extendsInterfaceClazz). OperationOnBar(extendsInterfaceClazz); } Not really related to the question, but I would suggest using reflection sparingly. It is very, very rarely a good solution.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions