22

Machine is defined as public enum Machine{...}

_machines is defined as private Machine[] _machines;

Don't know why this doesn't work:

_machines = {Machine.a, Machine.b};

error message:

illegal start of expression

Thank you guys!

2

2 Answers 2

49

You are missing one tiny part of the Array declaration.

_machines = new Machine[]{Machine.a, Machine.b};
Sign up to request clarification or add additional context in comments.

4 Comments

Machine.a, Machine.b can be simplified to a, b. The prefix is redundant, because an array of Machine enums can only contain Machine enums.
@ceving this does not work. Is there something I need to do to make this happen? I do not want to write "Machine" every time.
@PulkitAgarwal have a look at static imports. docs.oracle.com/javase/1.5.0/docs/guide/language/…
Ho do I find not the position of some enum on this array? _machines.indexOf(Machine.a) doesn't work :(
2

This can also be declared empty at first if you give it a size.

_machines = new Machine[size];

1 Comment

Note that this is different from OP’s case. This initialises an array with null values, whereas OP is asking about initialising it with predefined non-null values.

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.