I\'m trying to make a Tetris game and I\'m getting the compiler error
Shape is not an enclosing class
when I try t
One thing I didn't realize at first when reading the accepted answer was that making an inner class static is basically the same thing as moving it to its own separate class.
Thus, when getting the error
xxx is not an enclosing class
You can solve it in either of the following ways:
static
keyword to the inner class, orNo need to make the nested class as static but it must be public
public class Test {
public static void main(String[] args) {
Shape shape = new Shape();
Shape s = shape.new Shape.ZShape();
}
}
As stated in the docs:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
In case if Parent class is singleton use following way:
Parent.Child childObject = (Parent.getInstance()).new Child();
where getInstance()
will return parent class singleton object.
Shape shape = new Shape();
Shape.ZShape zshape = shape.new ZShape();
I have encountered the same problem. I solved by creating an instance for every inner public Class. as for you situation, i suggest you use inheritance other than inner classes.
public class Shape {
private String shape;
public ZShape zShpae;
public SShape sShape;
public Shape(){
int[][] coords = noShapeCoords;
shape = "NoShape";
zShape = new ZShape();
sShape = new SShape();
}
class ZShape{
int[][] coords = zShapeCoords;
String shape = "ZShape";
}
class SShape{
int[][] coords = sShapeCoords;
String shape = "SShape";
}
//etc
}
then you can new Shape(); and visit ZShape through shape.zShape;