runswim
runswim

Reputation: 33

Java Enum in constructor cannot be resolved to a variable

public static enum enuGender{
    MALE,
    FEMALE,
    UNKNOWN
}

public enuGender gender;

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Constructors &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
public Athlete(String firstName, String lastName, int age, enuGender gender) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.gender = gender;
}

I have this code in an "Athlete" class, however when I try to instantiate an athlete in my main class like so:

Athlete ath = new Athlete("Jim", "Denver", 33, enuGender.MALE);

I am told the enuGender cannot be resolved to a variable. Can I not pass an enum value in the constructor??

Upvotes: 3

Views: 3922

Answers (1)

rgettman
rgettman

Reputation: 178253

Presumably the enuGender enum is nested within the Athlete class. To refer to that enum from outside that class, you must specify the enclosing class name. Try

Athlete ath = new Athlete("Jim", "Denver", 33, Athlete.enuGender.MALE);

The alternative would be to pull that enum declaration out of the Athlete class so that specifying the enclosing class name would be unnecessary.

Also, enum names follow Java conventions for class names; Gender is a better name.

Upvotes: 9

Related Questions