Juan Gallardo
Juan Gallardo

Reputation: 104

On IntelliJ IDEA. Unable to add String to ArrayList<String>. Error: "Cannot resolve symbol 'add' "

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

Answers (1)

user10639668
user10639668

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

Related Questions