How to define a multidimensional array in Java using curly braces?
arrays, java, multidimensional-array
Solution
You need to add `new String[][]` before the array aggregate:
users = new String[][] { { email, name, pass }, {"one", "two", "three"} };
Problem
I don't understand the behaviour of Java about arrays. It forbids to define an array in one case but allows the same definition in another. The example from tutorial: ``` String[][] names = { {"Mr. ", "Mrs. ", "Ms. "}, {"Smith", "Jones"} }; System.out.println(names[0][0] + names[1][0]); // the output is "Mr. Smith"; ``` My example: ``` public class User { private static String[][] users; private static int UC = 0; public void addUser (String email, String name, String pass) { int i = 0; // Here, when I define an array this way, it has no errors in NetBeans String[][] u = { {email, name, pass}, {"one@one.com", "jack sparrow", "12345"} }; // But when I try to define like this, using static variable users declared above, NetBeans throws errors if (users == null) { users = { { email, name, pass }, {"one", "two", "three"} }; // NetBeans even doesn't recognize arguments 'email', 'name', 'pass' here. Why? // only this way works users = new String[3][3]; users[i][i] = email; users[i][i+1] = name; users[i][i+2] = pass; UC = UC + 1; } } ``` The mistakes thrown by NetBeans are: illegal start of expression, ";" expected, not a statement. And also it doesn't recognize arguments `email`, `name`, `pass` in the definition of the `users` array. But recognizes them when I define `u` array. What is the difference between these two definitions? Why the one works but the another one defined the same way doesn't?