Problems of enumerating types in Java
I'm new to Java programming and have some problems making enumeration types work In my program, I declare the following static variables:
class Employee { enum Gender {MALE,FEMALE}; static final double NORMAL_WORK_WEEK = 37.5; static int numberOfFemales; static int numberOfMales; Gender sex; }
I added a method to print relevant information and the following methods:
static void registerEmployeeGender(Gender sex) { switch(sex) { case MALE: numberOfMales++; break; case FEMALE: numberOfFemales++; break;} }
I cannot use the last method on the client where I run the program Suppose I create an object employee1 and enter:
Employee1.registerEmployeeGender(FEMALE);
Then I get an error message: female cannot be resolved to a variable
What caused this error message? As I said, I'm new to Java. This is my first attempt to use enumeration types, so I may have made a mistake I would appreciate it if anyone could give me any help
Of course, I only released some programs here, but this is the only part that gives me an error message If you need me to release more programs or all related content, please let me know
Thank you for your help!
Solution
use
Employee1.registerEmployeeGender(Gender.FEMALE);
And make sure you generate the following import statement in your code
import static com.example.Employee.Gender.*;