I don't understand why the compiler doesn't accept this code
import javax.swing.event.EventListenerList;
import java.util.EventListener;
public class GenericEventsManager {
EventListenerList listeners = new EventListenerList();
public <T extends EventListener> void addListener(T listener) {
listeners.add(listener.getClass(), listener);
}
}
The error I get is
The method add(Class<T>, T) in the type EventListenerList is not applicable for the arguments (Class<capture#1-of ? extends EventListener>, T)
The argument in addListener is of a type that extends EventListener, so listener.getClass() returns Class<? extends EventListener>
, which is exactly what the EventListenerList.add method expects
Can someone explain this? I have a feeling that it has something to do with getClass() not being resolved at compile time but it still doesn't make sense to me