Error
% javac StringTest.java
StringTest.java:4: variable errorSoon might not have been initialized
errorSoon[0] = "Error, why?";
Code
public class StringTest {
public static void main(String[] args) {
String[] errorSoon;
errorSoon[0] = "Error, why?";
}
}
You need to initialize
errorSoon, as indicated by the error message, you have only declared it.You need to initialize the array so it can allocate the correct memory storage for the
Stringelements before you can start setting the index.If you only declare the array (as you did) there is no memory allocated for the
Stringelements, but only a reference handle toerrorSoon, and will throw an error when you try to initialize a variable at any index.As a side note, you could also initialize the
Stringarray inside braces,{ }as so,which is equivalent to