I design a class:
public class CustomEvent< P, T >
{
/** Facade interface used for adopting user interfaces to our generic class. */
public interface ICaller< P, T >
{
/** callback facade method. */
void call( P parent, T callback, Object... objects );
}
/** Abstract class for simplifying naming in constructor. */
public abstract class Caller implements ICaller< P, T >{}
/** Constructor. */
public CustomEvent( final String name, final P parent, final Caller caller ){}
}
Now I want to create a instance of such class:
public class TestClass
{
private final TestEvent mEventOnLoad;
public TestClass()
{
// ERROR here: No enclosing instance of type CustomEvent<P,T> is accessible.
// Must qualify the allocation with an enclosing instance of type
// CustomEvent<P,T> (e.g. x.new A() where x is an instance of CustomEvent<P,T>).
mEventOnLoad = new TestEvent( "onLoad", this, new TestEvent.Caller() {
public void call( TestClass parent, ITestCallbacks callback, Object... objects )
{
// some code here
}
} );
}
private class TestEvent extends CustomEvent< TestClass, ITestCallbacks >
{
public TestEvent( String name, TestClass parent, TestEvent.Caller caller )
{
super( name, parent, caller );
}
};
}
Is it possible to somehow to workaround that? I want to simplify naming of the classes, instead of long generic declaration use short abstract class name which contains all needed type definitions?
I have a filling that it is possible... but I'm not very comfortable with Java yet...
Solution
private class TestEvent extends CustomEvent< TestClass, ITestCallbacks >
{
public final static TestEvent Instance = new TestEvent( null, null, null );
public TestEvent( String name, TestClass parent, TestEvent.Caller caller )
{
super( name, parent, caller );
}
};
public TestClass()
{
mEventOnLoad = new TestEvent( "onLoad", this, TestEvent.Instance.new Caller() {
public void call( TestClass parent, ITestCallbacks callback, Object... objects )
{
// some code here
}
} );
}