I´m trying to extend a Java ArrayList to support events as described in this example:
http://www.exampledepot.com/egs/java.util/CustEvent.html
The problem is that java is reporting that ‘Cannot make a static reference to the non-static type MyEvent’ in the following line:
public interface MyEventListener extends EventListener {
public void myEventOccurred(MyEvent evt);
}
I think that is something related to Generics, but I really don´t know how to solve this issue. Does anybody can help me?
Here is the full source of what I´m trying to do:
public class ArrayList<E> extends java.util.ArrayList<E> {
private static final long serialVersionUID = 1L;
// Declare the event. It must extend EventObject.
public class MyEvent extends EventObject {
private static final long serialVersionUID = 1L;
public MyEvent(Object source) {
super(source);
}
}
// Declare the listener class. It must extend EventListener.
// A class must implement this interface to get MyEvents.
public interface MyEventListener extends EventListener {
public void myEventOccurred(MyEvent evt); //Error occurs here
}
}
There’s a more complex solution and a simpler one. The simpler one: Don’t use inner classes or interfaces here; there’s simply no need, and they only complicate matters. As an aside, why is your class given the same name as the core class? If that compiles (and I’m guessing it does), it’s highly confusing to all.
Edit: and in your link, none of those classes or interfaces are meant to be inner, so you’ve transcribed it wrong. Again, put each class and interface in its own file. There are times when you will want to use inner classes, but this is most definitely not one of them.