Code:
String Foo[];
Foo={"foo","Foo"};
Error at Line 2: Illegal Start of expression
The code works if I say:
String Foo[]={"foo","Foo"};
Why does this happen, and how should I do the required without generating an error? This also happens with other data types.
Would appreciate if you could explain in layman terms.
{"foo", ...}is meant to be an array, so usenew String[]{"foo", ...}. Besides that, do yourself a favor and let variable names start with a lower case character, i.e.String fooinstead ofString Foo(and I personally like to have the array declaration at the type, i.e.String[] foo).InitializationandAssignmentthese are two words that makes the difference. I believe. :)List<String> list = List.of( "foo" , "bar" ) ;as a single line or two lines.new String[] { ...}(this was an always must in earlier versions of Java), but, as a syntactic sugar, the compiler accepts just{ ... }in a field declaration or variable declaration. Why? it is specified that way in the Java Language Specification (probably it would be to messy to accept it everywhere)