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?

1
  • 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.) Commented Jan 24, 2016 at 1:23

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Didn't think of this, but it seems very reasonable and works like a charm
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().

Comments

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.

Comments

1

Below should do it

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.