简体   繁体   中英

Import cannot be resolved?

So right now, I have a Brick.java which contains a Levels enum that I want to use in BreakoutCourt.java, which is contained in the same package, which is (default package).

When I write import Brick.level; in BreakoutCourt , I get a message that says The Import Brick Cannot Be Resolved. I get that message even if I write import static Brick.Level!

The levels enum contained in Brick.java looks like this:

public class Brick {
   public static final int BWIDTH = 60;
   public static final int BHEIGHT = 20;
   private int xPos, yPos; 
   private Level brickLevel;

    //This sets up the different levels of bricks.
   enum Level{
    LUNATIC (4, 40, Color.MAGENTA),
    HARD (3, 30, Color.PINK), 
    MEDIUM (2, 20, Color.BLUE),
    EASY (1, 10, Color.CYAN),
    DEAD (0, 0, Color.WHITE);
    private int hitpoints;
    private int points;
    private Color color;

    Level(int hitpoints, int points, Color color){
        this.hitpoints = hitpoints;
        this.points = points;
        this.color=color;
        }
    public int getPoints(){
        return points;
        }
    public Color getColor(){
        return color;
        }
}

//rest of brick class goes under the enum

And I use it in BreakoutCourt like this:

    //Generates the bricks.
    for(int i = 0; i < 8; ++i){
        ArrayList<Brick> temp = new ArrayList<Brick>();
        Level rowColor = null;
        switch(i){
        //There are two rows per type of brick.
            case 0:
            case 1:
                rowColor = Level.EASY;
                break;
            case 2:
            case 3:
                rowColor = Level.HARD;
                break;
            case 4:
            case 5:
                rowColor = Level.LUNATIC;
                break;
            case 6:
            case 7:
            default:
                rowColor = Level.MEDIUM;
                break;
        }
        for(int j = 0; j < numBrick; j++){
            Brick tempBrick = new Brick((j * Brick.BWIDTH), ((i+2) * Brick.BHEIGHT), rowColor);
            temp.add(tempBrick);
        }

What am I doing wrong? Thank you for the help!

If you want to import a member of a class, you need to use a static import . So you could do:

import static Brick.Level;

Be careful though. Static imports should be used sparingly, as noted by that linked page. Another way to do it without a static import is to use the outer class name. For example: Brick.Level.LUNATIC The reason is that in a larger project you might have multiple classes with a Level enum, and you would have to look at the import to see which one is being used.

How are you importing Enum ?

You should have an import statement like

import static Brick.Level

Related question

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