0

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); } } 
4
  • What do your import statements look like? Commented Mar 4, 2015 at 2:03
  • i don't have any import statements. They're all in the same directory should'nt the files be able to see each other. Commented Mar 4, 2015 at 2:13
  • 1
    Just for the record: previous question. Commented Mar 4, 2015 at 2:15
  • Whic java version you are using. Before java 5 Enum was not supported. Commented Mar 4, 2015 at 2:25

1 Answer 1

6

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

4 Comments

So I have several classes that are connected that implement the Faction enum. So do I have to go back and import it in every class I want to use it in?
@Josephhooper Either that or refactor your enum type by extracting it to its own .java file within the same package as all your other classes.
Wait since all the classes are in the same folder doesn't that mean they should all be in the same package?
@Josephhooper Not necessarily. The package is declared with a package declaration at the top of the source file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.