PDA

Click to See Complete Forum and Search --> : Enumerations in Java Help


GamerMax5
May 2nd, 2007, 06:27 PM
I haven't done Java in so long and coming back to it from what seems like years of C++ programming isn't helping any at all.

I'm stuck on enumerations, which are nothing at all like enumerations in C++. When creating an FSM in C++, I could simply use an enumeration to store the states of the machine and in the main loop of the program, I could query the variable that stored the current state and when needed, I could change the state and then accomidate the actions of the program depending upon the value of that variable.

I'm attempting a similar program in Java however I'm running into a lot of problems. For instance, I set up an enumeration with application states like such:

private enum AppStates {
STATE_MAIN_MENU,
STATE_ADD_ITEM,
STATE_ADD_ITEM1,
STATE_ADD_ITEM2,
STATE_ADD_ITEM3,
STATE_APP_STOP
}

I'm almost positive that's the appropriate way to declare an enumeration.

Next I have a variable to store the application states in so that I can read the current state and change it as needed:

private AppStates CurrentState = AppStates.STATE_MAIN_MENU;

Again, I'm not too sure but I think this is the appropriate declaration for this type of variable.

This is where a large deal, in fact all, of my problems are coming from. Inside the main loop I have a while loop controlling the shutdown of the program (visa-vi while AppState isn't in Shutdown State). Then I have a switch structure testing the current value of the variable that holds the application's current state as such:

//Always query the application's state
switch(CurrentState) {
case AppStates.STATE_MAIN_MENU:
//Display the main menu
DisplayMainMenu();
break;
case AppStates.STATE_ADD_ITEM:
//Brings the user to the add item menu
DisplayAddItemMenu();
break;
case AppStates.STATE_ADD_ITEM1:
//Adds an item with no information provided
break;
case AppStates.STATE_ADD_ITEM2:
//Adds an item with only the name and description provided
break;
case AppStates.STATE_ADD_ITEM3:
//Adds an item with everything provided
break;
case AppStates.STATE_APP_STOP:
//This is where the app stops at
break;
}

The compiler keeps complaining about the start of the switch structure saying that it can't make a static reference to a non-static field. I'm seriously drawing blanks here and am going brain dead. Can someone help me out here? Many thanks in advance.

System_Error
May 4th, 2007, 12:46 PM
The compiler keeps complaining about the start of the switch structure saying that it can't make a static reference to a non-static field.

Is the switch structure in a static method?

CornedBee
May 4th, 2007, 02:42 PM
And if yes, you might want to make the enum and the variable that is of enum type static. Or create an instance of the class and put the switch in a non-static method.

Also, in switches on enums, you don't need to (and AFAICR can't) prefix the constants, i.e. it's just
switch(appState)
{
case MAIN_MENU:

And another thing I've hinted at: because of the enum constants' name lookup rules, giving them a common prefix ought to be unnecessary.