Problem with enumerated type in Java
I am very new to Java-programming, and have some problems in getting an enumerated type to work. In my program I have declared 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 have added a method for printing relevant information, as well as the following metho开发者_StackOverflow中文版d:
static void registerEmployeeGender(Gender sex) {
switch(sex) {
case MALE:
numberOfMales++; break;
case FEMALE:
numberOfFemales++; break;}
}
In my client where I attemt to run the program, I am unable to use this last method. Say I create an object, Employee1, and type:
Employee1.registerEmployeeGender(FEMALE);
I then get the error message: FEMALE cannot be resolved to a variable.
What is causing this error message? Like I said, I am quite new to Java, and this is my first attempt at using the enumerated type, so I probably have done something wrong. If anyone can give me any help, I would greatly appreciate it.
Of course, I have only posted part of the program here, but this is the only part that is giving me an error message. If you need me to post more of the program, or all of it for that matter, please let me know.
Thanks in advance for any help!
use
Employee1.registerEmployeeGender(Gender.FEMALE);
and make sure that u make the following import statement in your code
import static com.example.Employee.Gender.*;
You need to static import
the enum values in order to be able to use them statically the way as you presented:
import static com.example.Employee.Gender.*;
The normal practice, however, is to just import the enum
import com.example.Employee.Gender;
and specify the enum name as well:
Employee1.registerEmployeeGender(Gender.FEMALE);
To access the enum items use it like this "Gender.FEMALE"
This might help you more.
精彩评论