Q:

Java: Why does the compiler complain here?

  0          int specialFuncStart;
  1          String[] specialFunctions= {"sum","ddx","integral"};
  2          for(String element: specialFunctions){
  3              specialFuncStart = finalMath.lastIndexOf("sum");
  4          }
  5          while (specialFuncStart != -1) {   code  }

Why does line 5 say that specialFuncStart might not have been initialized? Strings are final and fixed, so the for loop will always run. Does the compiler not know that or am I missing something? I understand that initializing specialFuncStart = -1; is how to fix it, but why?

A:

You declared it but you didn't initialize it. You need to set a value to it:
int specialFuncStart; // declare
specialFuncStart = 0; // initialize

int specialFuncStart = 0; // both

Your compiler thinks that sometimes specialFuncStart will not be initialized since a for loop doesn't have to execute.

