why do I get a compilation error if I try to initialize an array using braces on a statement other than the declaration statement
class Demo {
public static void main(String[] args) {
String[] myName;
myName = {"AA", "BB", "CC"};
for(String s: myName) {
System.out.print(s + " ");
}
}
}
what's the point of having to initialize using braces only in the declaration statement?
demo.java:4: error: illegal start of expression
myName = {"AA", "BB", "CC"};
^
demo.java:4: error: not a statement
myName = {"AA", "BB", "CC"};
^
demo.java:4: error: ';' expected
myName = {"AA", "BB", "CC"};
^
demo.java:5: error: illegal start of type
for(String s: myName) {
^
demo.java:5: error: ')' expected
for(String s: myName) {
^
demo.java:5: error: <identifier> expected
for(String s: myName) {
^
demo.java:10: error: class, interface, or enum expected
}
^
7 errors