1

I experimented a little with enums and arrays in Java:

enum animals
  {CAT, DOG, COW, BIRD, POTATO}

To get this into an array I simply do this...

animals[] creatures = {animals.CAT, animals.POTATO};

But now if I have to define bigger entries, I thought it would be useful if I can just type in the enumeration without animals.XXX, like I also do in C++

animals[] creatures = {CAT, POTATO, BIRD, CAT, CAT, POTATO, COW...}
  • It would take time and gives me a better overview
  • Java already knows that my target type is 'animals', so in my opinion this is unnecessary (?)

So I just wondered if this is possible in any way, and why if not?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Jojo
  • 41
  • 1
  • 6
  • Note also that there is an apparent discrepancy between what you wrote above and the `switch` syntax for enums: the `cases` in such switch statements _cannot_ be `case animals.CAT:` etc. but instead they _must_ be `case CAT:` unlike their use in an array initializer. So you could augment your question by asking, if I can (in fact must) leave out the qualifier in `case` statements why can't I do the same in array initializers? (And vice versa of course.) – Klitos Kyriacou Jan 24 '16 at 01:23

4 Answers4

4

Java mandates that you use the fully classified name to use it. For standard classes, this is generally done by importing a package with import name.of.package and then using the simple name of the class. For static fields, like enum constants, you can use static imports.

import static animals.CAT;
import static animals.POTATO;
import static animals.BIRD;
import static animals.COW;

// ...

animals[] creatures = {CAT, POTATO, BIRD, CAT, CAT, POTATO, COW...};

You could also use

import static animals.*;

avoiding the need to import every animals, but note that the first construct is generally considered better than the second one.

Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Thank you! Didn't think of this, but it seems very reasonable and works like a charm – Jojo Jan 24 '16 at 01:23
2

While there are other solutions that will work, you don't have to really go through any of that. What you can do instead is use the values() array to pull in all of the present values attached to the array instead.

That is, if you really want to have an array called creatures that contains all entries of your enum, you can express that as this:

// Classes, interfaces, and enums should start with an upper case
Animals[] creatures = Animals.values();

If you add a new entry to the enum it will be automatically pulled in through values().

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

First off, the enum should be named Animals, since types should start with an uppercase letter.

The reason you have to qualify the values in the array initializer, is because each value of the initializer is a full-blown expression.

int x = <expression>;
int[] x = {<expression>, <expression>, ...};
Animals x = <expression>;
Animals[] x = {<expression>, <expression>, ...};

The only place an enum value doesn't need to be qualified, is in a switch statement, because the value of a case must be a constant, not an expression:

switch (animal) {
    case CAT:
        // code
        break;
    case DOG:
        // code
        break;
}

Outside of that you have to qualify, unless of course you import the enum values using import static.

Andreas
  • 154,647
  • 11
  • 152
  • 247
1

Below should do it

animals[] creatures = {animal.CAT, animal.POTATO, animal.BIRD, CAT};
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173