Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum Inside Class

Tags:

java

enums

I am trying to define an enum within a class but Eclipse continues to underline my code in the lovely red color. Here is what my code looks like for the most part:

package campusvilleFoodBank;

import java.util.NoSuchElementException;
import java.util.Scanner;

public class FoodArrayList {

    public enum Days {SUN, MON, TUE, WED, THUR, FRI, SAT}

    private Food [] foodArray;
    private int iterator;
    private boolean startIndexing;
    private static final int MAX = 24;

    public FoodArrayList(){
        foodArray = new Food [MAX];
        iterator = 0;
    }

    public FoodArrayList(int arraySize){
        foodArray = new Food [arraySize];
        iterator = 0;
    }
//Rest of code, with more complicated methods here
}

I must be missing something very simple, it's likely to be a syntax error.... The code that follows involves methods such as my toString(), some sorting methods for the foodArray instance variable, and things along those lines. I do not have a main method defined. I know that I can define an enum within a class definition and all of the code I've seen elsewhere is written nearly exactly the same as to what I have. However, Eclipse does not seem to recognize even the enum declaration and I don't believe I need to import anything.

Here is what I see in Eclipse for the code I have an error with:

Code with error

For comparison, here is some enum code from another, separate .java file that appears to be without error:

Code without error

Any assistance on this would be appreciated. Let me know if further clarification is needed.

like image 573
coolDude Avatar asked Nov 08 '22 22:11

coolDude


1 Answers

It could be a couple of things but not related to enum, this is fine.

  1. Seems to me that unless Food does exist, it MUST be created
  2. If it does exist, then it must reside in a different package, which in turn you have to import it like this

    import package_name.package2.Food;

  3. It could be that your class name is not identical to you file name

    FoodArrayList

  4. Sometimes your IDE in this case Eclipse needs some help, try saving the file, closing it and opening it back up.

like image 186
Esteban Rincon Avatar answered Nov 15 '22 12:11

Esteban Rincon