As defined in JLS, the first “part” of the for
loop declaration, ForInit
, is a list of statement expressions or a local variable declaration; j
isn’t a statement expression (an assignment; a pre/post increment/decrement; a method invocation; a new class initialization) or a local variable declaration, so it’s invalid syntax.
Depending upon what you are trying to do, you can just omit it:
for (; j < 8; j++)
Note that all of the three “parts” of the for
loop are optional: for instance, this is fine:
for (;;) {
}
(It’s just an infinite loop)
solved why outer for loop variable can’t be used in inner for loop