Reputation: 104
static List<String> commands = new ArrayList<>(asList("Hello", "Goodbye"));
commands.add("Hi!");
Compiler errors:
Error:(37, 17) java: <identifier>
expected
Error:(37, 18) java: illegal start of type
Those locations are after and before the open parenthesis on .add();
Upvotes: 0
Views: 968
Reputation:
Something like this:
class MyClass {
static List<String> commands = new ArrayList<>(asList("Hello", "Goodbye"));
commands.add("Hi!");
}
is illegal. You cannot call a method inside class definition.
To make it work use a static block:
class MyClass {
static List<String> commands = new ArrayList<>(asList("Hello", "Goodbye"));
static {
commands.add("Hi!");
}
}
Upvotes: 3