This compiles and works:
public class Test {
public static int[] array;
public static void main(String[] args) {
int[] temp = {42};
array = temp;
}
}
This doesn’t:
public class Test {
public static int[] array;
public static void main(String[] args) {
array = {42};
}
}
Why is this? How can I assign arrays to non-local variables without using a temporary local variable?
The variable has already been declared. You need to assign it thus: