0

I've got a constructor like this:

public Cat(String name, String[][] friendsOfFriendsNames){...}

And I would like to create a new Cat like this:

Cat cat = new Cat("Maurycy", {{"Adam", "Greta"}, {"Jurek", "Tyrmand"}});

However I am getting Syntax error on token(s), misplaced construct(s) error in Eclipse.

It turns out that I can introduce a new variable like friendsOfFriendsNames and then pass it to the constructor. Eclipse doesn't raise any error then.

String[][] friendsOfFriendsNames = {{"Adam", "Greta"}, {"Jurek", "Tyrmand"}};

Cat cat = new Cat("Maurycy", friendsOfFriendsNames);

Why is it so? Is it possible to pass {{"Adam", "Greta"}, {"Jurek", "Tyrmand"}} directly to the constructor? If so, how should I do it?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79

2 Answers2

6

You have to tell Java what type of array it is, e.g.

Cat cat = new Cat("Maurycy", new String[][]{{"Adam", "Greta"}, {"Jurek", "Tyrmand"}});
durron597
  • 31,968
  • 17
  • 99
  • 158
0

Try like this:

new String[][]{{"Adam", "Greta"}, {"Jurek", "Tyrmand"}}
brso05
  • 13,142
  • 2
  • 21
  • 40