Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with enumerated type in Java

Tags:

java

enumerate

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 method:

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!

like image 424
Kristian Avatar asked Jan 22 '26 01:01

Kristian


1 Answers

use

Employee1.registerEmployeeGender(Gender.FEMALE);

and make sure that u make the following import statement in your code

import static com.example.Employee.Gender.*;
like image 142
confucius Avatar answered Jan 23 '26 15:01

confucius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!