简体   繁体   中英

Java cannot find symbol for class in another java file but in the same directory

I am a beginner in learning java, and I am using notepad to learn my basics in java and I can't seem to use the class that I have in another java file(but in the same directory), could someone teach me how to do this ?

For example, in the same directory, I have 2 java files, one is called Shape.java, another is Board.java. In my Shape.java, I have a class called Tetrominoes, now how do I use this Tetrominoes class in my Board.java. I know using import Shape.Tetrominoes will not work.

Below I posted some of the codes that is connected to the class I'm talking about.

Shape.java

public class Shape {
    protected enum Tetrominoes { NoShape, OtherShape };
    private Tetrominoes pieceShape;

    public Shape() {
        setShape(Tetrominoes.NoShape);
    }

    public void setShape(Tetrominoes shape) {
        // Codes...
    }

    public Tetrominoes getShape() { return pieceShape; }

}

Board.java

public class Board extends JPanel implements ActionListener {
    private Tetrominoes[] board;

    public Board(Game parent) {
        initBoard(parent);
    }

    // and other codes...
}

Everything is fine, except for this error that says "cannot find symbol" pointing to "private Tetrominoes[] board;"

Why is this ? and how should I fix this ? Thank you in advance. Please ask me if anything is unclear or what else I needed to post, I will update my question as soon as I can.

Your Tetrominoes enum is nested in Shape . So you could refer to it as:

private Shape.Tetronminos[] board;

... but you'd be better off promoting it to a top-level type, in my view. I'd also suggest calling it Tetromino (singular) and making the value names follow Java naming conventions: NO_SHAPE and OTHER_SHAPE (although I assume those won't be the real values anyway).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM