Is not an enclosing class Java

V Sebi picture V Sebi · Nov 27, 2013 · Viewed 328.7k times · Source

I'm trying to make a Tetris game and I'm getting the compiler error

Shape is not an enclosing class

when I try to create an object

public class Test {
    public static void main(String[] args) {
        Shape s = new Shapes.ZShape();
    }
}

I'm using inner classes for each shape. Here's part of my code

public class Shapes {
    class AShape {
    }
    class ZShape {
    }
}

What am I doing wrong ?

Answer

Peter Lawrey picture Peter Lawrey · Nov 27, 2013

ZShape is not static so it requires an instance of the outer class.

The simplest solution is to make ZShape and any nested class static if you can.

I would also make any fields final or static final that you can as well.