1

I would like to make abstract class which has several inner classes extend it and could create instances of its inner classes via static methods but I get compiler error "No enclosing instance of type ITask is accessible. Must qualify the allocation with an enclosing instance of type ITask (e.g. x.new A() where x is an instance of ITask)."

I've found out that inner classes should be created by instances of outer class but my outer class has abstract method and I can't create instance of it. I made inner classes which extend parent because I wont to control creation of them. So is there any way how to make my pattern works.

My code:

public abstract class ITask {
public abstract void Execute(Subscriber itm);

static public ITask CreateSendTask(Buffer buffer)
{
    return new SendData(buffer);
}

static public ITask CreateSTTask(Ticket sid)
{
    return new StartTransmission(sid);
}

static public ITask CreateETTask(Ticket sid)
{
    return new EndTransmission(sid);
}


private class SendData extends ITask
{
         /// some implemetation...
}

private class StartTransmission extends ITask
{
         /// some implemetation...
}

private class EndTransmission extends ITask
{
    /// some implemetation...
}

}

The problem is with methods Create(.*)Task.

Thanks!

  • See http://stackoverflow.com/questions/7901941/no-enclosing-instance-of-type-server-is-accessible – Raedwald Mar 02 '16 at 22:31
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 03 '16 at 23:18

1 Answers1

3

The inner classes should be made static if they're created from static methods, and thus don't need access to an enclosing ITask.

You should also respect Java naming conventions: methods start with a lower-case letter.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • What does it mean static class in java? How it will be located in memory? Is it possible to have members in such class? – Danil Krivopustov Jun 15 '12 at 07:08
  • 1
    static inner classes are just like regular, top-level classes, except their name is EnclosingClass.InnerClass. Read http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – JB Nizet Jun 15 '12 at 07:13