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...?)
Try this to get it working as you might have intended it:
public class weirdStringArray {
String [] method1 = new String [] {"one","two","three"}; //<-- Works no matter where it's placed, because this is a valid one line initialisation
String [] method2;
{
// We gave it the '{' the compiler asked for …
method2 = new String[3];
method2[0] = "one";
method2[1] = "two";
method2[2] = "three";
}
String [] method3 = new String[3];
{
method3[0] = "one";
method3[1] = "two";
method3[2] = "three";
} // <-- … and this is the missing '}'
}
These '{…}' blocks are initialiser blocks, better known/more often used in their static variant, as 'static {…}' for static final attributes. That's because the code in those initialisers is usually better placed into a constructor.
The advantage of using these initialisers is that they are executed before all constructors, the disadvantage is that they are executed before all constructors. Best seen when you play with final attributes …
They work fine for all constant initialisations, like in the sample here, and other types of (potentially) never failing initialisations. Calling non-static methods from here should be avoided, and calling non-static methods of the current class does not work at all (this was not yet initialised as you are currently initialising it …).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With