What happened to my factory?

I have code like this:

public abstract class Foo {
    public static Foo getFoo() {
        return new FooImpl();
    }

    abstract void DoFoo();

    private class FooImpl extends Foo {
        public FooImpl() { }

        @Override
        void DoFoo() { }
    }
}

But Eclipse is telling me. No enclosing instance of type Foo is accessible.So, how can I get this to work?

I tried to make it as simple as possible to see if it will compile:

public abstract class Foo {
    public static Foo getFoo() {
        return new FooImpl();
    }

    private static class FooImpl extends Foo {
        public FooImpl() { }
    }
}

And I still get the same error. What am I missing?

CORRECTED! I changed the line return new FooImpl();toreturn new Foo.FooImpl();

+3
source share
3 answers

- , FooImpl static, , ( ). getFoo , static, btw - , Foo ?

+9

getFoo()?

- , static.

+2

FooImpl static, .

+1

Source: https://habr.com/ru/post/1725012/


All Articles