Question

I have defined the static inner class Tetromino but I am getting a compile error in the line:

Tetrominoes.add(tetr);

and I cannot figure out why. Am I missing something blatantly obvious?

import java.util.ArrayList;

public class Tetris{

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    static class Tetromino
    {

        ArrayList<Tetromino> Tetrominoes = new ArrayList<Tetromino>();

        Tetromino tetr = new Tetromino();

        Tetrominoes.add(tetr); //This line generates an error

    }

}

In Eclipse, it underlines the line I stated above with red, however when I compile, It says:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at Tetris.main(Tetris.java:5)

where line 5 is my declaration of the main method.

Was it helpful?

Solution

This statement should be added in an non-static initializer, method or constructor:

For example, the case with the constructor will look like this:

public Tetromino() {
    Tetrominoes.add(tetr);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top