Why is it getting an error in Java?

Asked 2 years ago, Updated 2 years ago, 58 views

I'm making a Tetris game. If you execute the code below, the message Shape is not an enclosing class appears.

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

    private String shape;
    private int[][] coords;
    private int[][] noShapeCoords = { { 0, 0 },   { 0, 0 },   { 0, 0 },   { 0, 0 } };
    private int[][] zShapeCoords = { { 0, -1 },  { 0, 0 },   { -1, 0 },  { -1, 1 } };
    private int[][] sShapeCoords = { { 0, -1 },  { 0, 0 },   { 1, 0 },   { 1, 1 } };
    private int[][] lineShapeCoords = { { 0, -1 },  { 0, 0 },   { 0, 1 },   { 0, 2 } };
    private int[][] tShapeCoords = { { -1, 0 },  { 0, 0 },   { 1, 0 },   { 0, 1 } };
    private int[][] squareShapeCoords = { { 0, 0 },   { 1, 0 },   { 0, 1 },   { 1, 1 }   };
    private int[][] lShapeCoords = { { -1, -1 }, { 0, -1 },  { 0, 0 },   { 0, 1 } };
    private int[][] mirroredLShapeCoords = { { 1, -1 },  { 0, -1 },  { 0, 0 },   { 0, 1 } };

    public Shape(){
        int[][] coords =  noShapeCoords;
        shape = "NoShape";
    }

    class ZShape{
        int[][] coords =  zShapeCoords;
        String shape = "ZShape";
    }

    class SShape{
        int[][] coords = sShapeCoords;
        String shape = "SShape";
    }

 //etc
}

What's wrong?

class java

2022-09-21 18:43

1 Answers

ZShape is not a static class. This requires an instance of an external class. A simple solution is to define ZShape as static or to create and use instances of Shape and ZShape.


2022-09-21 18:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.