I have three different ways of declaring my string array in Java. One works all the time but the other two only work depending upon the order in which they are written.
In the first version, method2 does not work but method3 does.
public class weirdStringArray {
String [] method1 = new String [] {"one","two","three"}; //<------ Works no matter where it's placed
String [] method2; //<------ "Syntax error on token ";", { expected after this token"
method2 = new String[3]; // Doesn't work in this postion
method2[0] = "one";
method2[1] = "two";
method2[2] = "three";
String [] method3 = new String[3]; //<------- No issue, works fine in this position
method3[0] = "one";
method3[1] = "two";
method3[2] = "three";
} // <----- Syntax error, insert "}" to complete ClassBody (but it does seem to complete ClassBody...?)
But, switch positions and the working declarations swap too. Look, now method2 works but method3 doesn't.
public class weirdStringArray {
String [] method1 = new String [] {"one","two","three"};
String [] method3 = new String[3]; //<------ "Syntax error on token ";", { expected after this token"
method3[0] = "one"; // Doesn't work in this postion
method3[1] = "two";
method3[2] = "three";
String [] method2; //<---------- Put it in a different place and it works
method2 = new String[3];
method2[0] = "one";
method2[1] = "two";
method2[2] = "three";
} // <----- Syntax error, insert "}" to complete ClassBody (but it does seem to complete ClassBody...?)
What might be happening here ? Why would the order make any difference ? What's happening in position 2 ? Incidentally, it makes no difference if I remove the first working form:
public class weirdStringArray {
//String [] method1 = new String [] {"one","two","three"};
String [] method2; //<------ "Syntax error on token ";", { expected after this token"
method2 = new String[3]; // Doesn't work in this postion
method2[0] = "one";
method2[1] = "two";
method2[2] = "three";
String [] method3 = new String[3]; //<------- No issue, works fine in this position
method3[0] = "one";
method3[1] = "two";
method3[2] = "three";
} // <----- Syntax error, insert "}" to complete ClassBody (but it does seem to complete ClassBody...?)