简体   繁体   中英

Can't use enum in Java (Error:Can't find symbol)

So I have a class file with just my enums that looks like this

public class FactionNames {
    public enum Faction {AMITY, ABNEGATION, DAUNTLESS, ERUDITE, CANDOR};
}

I have a class that uses these enums in a constructor which looks like this

public Dauntless(String f, String l, int a,  int ag, int end, Faction d) {
        super(f, l, a, d);
        if (ag >= 0 && ag <= 10) {
            this.agility = ag;
        } else {
            this.agility = 0;
        }
        if (end >= 0 && end <= 10) {
            this.endurance = end;
        } else {
            this.endurance = 0;
        }
    }

So to make sure everything in this class works properly I want to create some Dauntless objects in a driver, but I keep getting these errors

D:\Documents\Google Drive\Homework\1331
Test.java:3: error: cannot find symbol
        Faction test;
        ^
  symbol:   class Faction
  location: class Test
Test.java:4: error: cannot find symbol
        test = Faction.DAUNTLESS;
               ^
  symbol:   variable Faction
  location: class Test
2 errors 

I'm using my driver which looks like this. Is there anything wrong with my syntax? I can't figure out why i'm getting this errors.

public class Test {
    public static void main(String[] args) {
        Faction test; 
        test = Faction.DAUNTLESS;
        Dauntless joe = new Dauntless("Joseph", "Hooper", 20, 5, 3, test);
        Dauntless vik = new Dauntless("Victoria", "Ward", 19, 6, 2, test);
        Dauntless winner;
        winner = joe.battle(vik);
        System.out.println(winner);

    }
}

The enum type Faction is nested within the top level class FactionNames .

public class FactionNames {
    public enum Faction {AMITY, ABNEGATION, DAUNTLESS, ERUDITE, CANDOR};
}

If you want to use its simple name, you'll need to import it

import com.example.FactionNames.Faction;

Alternatively, you can use its qualified name

FactionNames.Faction test = FactionNames.Faction.DAUNTLESS;

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