Note that there are some explanatory texts on larger screens.

plurals
  1. POJava compiler gives error - No enclosing instance of type
    primarykey
    data
    text
    <p>I design a class:</p> <pre class="lang-java prettyprint-override"><code>public class CustomEvent&lt; P, T &gt; { /** Facade interface used for adopting user interfaces to our generic class. */ public interface ICaller&lt; P, T &gt; { /** callback facade method. */ void call( P parent, T callback, Object... objects ); } /** Abstract class for simplifying naming in constructor. */ public abstract class Caller implements ICaller&lt; P, T &gt;{} /** Constructor. */ public CustomEvent( final String name, final P parent, final Caller caller ){} } </code></pre> <p>Now I want to create a instance of such class:</p> <pre class="lang-java prettyprint-override"><code> public class TestClass { private final TestEvent mEventOnLoad; public TestClass() { // ERROR here: No enclosing instance of type CustomEvent&lt;P,T&gt; is accessible. // Must qualify the allocation with an enclosing instance of type // CustomEvent&lt;P,T&gt; (e.g. x.new A() where x is an instance of CustomEvent&lt;P,T&gt;). 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&lt; TestClass, ITestCallbacks &gt; { public TestEvent( String name, TestClass parent, TestEvent.Caller caller ) { super( name, parent, caller ); } }; } </code></pre> <p>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?</p> <p>I have a filling that it is possible... but I'm not very comfortable with Java yet... </p> <p><strong>Solution</strong></p> <pre class="lang-java prettyprint-override"><code> private class TestEvent extends CustomEvent&lt; TestClass, ITestCallbacks &gt; { 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 } } ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload