4

I'm trying to crate my own thread safe event handling but I get E cannot be resolved to a type error on class decleration line below. How can I fix this?

Observers.java

public final class Observers<T extends EventHandler<E>> {

    private CopyOnWriteArrayList<T> mListeners = new CopyOnWriteArrayList<T>();

    public interface EventHandler<E> {
       public void HandleEvent(Object sender, E e);
    }

    /*...*/

    public void dispatchEvent(Object sender, E args) {
        /*...*/
    }
}

EventHandler.java

public interface EventHandler<E extends EventArgs> {
 /* ... */
}

1 Answer 1

12

You've only actually declared a single type parameter in Observers. Try this:

public final class Observers<E extends EventArgs, T extends EventHandler<E>> {

Note that it looks highly odd for your Observers class to declare its own nested EventHandler interface while implementing the outer one - if you really need both of those, I would suggest you rename one of them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.