Results 1 to 3 of 3

Thread: Enumerations in Java Help

  1. #1

    Thread Starter
    Hyperactive Member GamerMax5's Avatar
    Join Date
    Nov 2004
    Location
    United States
    Posts
    388

    Question Enumerations in Java Help

    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:

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

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

    Code:
    		//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.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Enumerations in Java Help

    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?

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Enumerations in Java Help

    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
    Code:
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width