As we all know, in Java you can combine an array declaration with array initialization, as follows:
But why is it that such a statement will not work if declared within the bounds of an IF statement? I had never heard before that variables cannot be declared within if statements! The code segment is as follows:
The code returns a compile error for both lines, saying:
".class expected" and "not a statement" for both of the array declarations/initializations. Could someone tell me why?
Thanks
Code:
n1 = 5;
double[] g = new double[n1 + 1];
Code:
n1 = 5;
n2 = 7;
if (n1 > n2)
double[] f = new double[n1 + 1];
else
double[] f = new double[n2 + 1];
".class expected" and "not a statement" for both of the array declarations/initializations. Could someone tell me why?
Thanks