1

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.

3
  • You can't put method calls directly in the body of a class. Commented Feb 4, 2014 at 16:28
  • 2
    Never run a program that has compiler error. Commented Feb 4, 2014 at 16:28
  • Why shouldn't you ever run a program with a compiler error? Commented Feb 4, 2014 at 16:47

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.