0

I created the enumeration class and initialization class, but when I went to implement these changes I tried adding objects of type enumType to an array list of objects.

public enum inputType {
    POLNBR, NAME, CLMNBR, PHN;
}

public class enumType {
    inputType type;

    public enumType(inputType type){

        this.type = type;
    }
}

What I don't understand is why I can only get it to work in a static way. Meaning I was only able to add to the array list by using the enumeration class attribute instead of instantiating an object of said class.

private List<inputType> possibilities = new ArrayList<inputType>();

Then later I add to the array with:

possibilities.add(inputType.POLNBR);

I was able to add an object, but I was not able to return the object attribute, so this is the only way I was able to make it work.

  1. Is this the correct way to use enum classes??

  2. Is there a way to add the object and later return the object with the attributed enum value?

Sorry, I have never tried to use them, and I am trying to add them to my repertoire.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason V
  • 623
  • 1
  • 10
  • 30

1 Answers1

2

You were using enums correctly before trying using an ArrayList. To set the value of the enum inputType in your enumType object all you have to do is create a new instance of the enumType object by calling the constructor, here's a quick example:

enumType e = new enumType(inputType.NAME);
// You can also replace it with the value you want

For more information about enums in Java please refer to this.

mrhallak
  • 1,138
  • 1
  • 12
  • 27