Places where an expression can occur as per the JLS?
That clause is actually true, though it is not stated in a precise way.
For example:
-
a=33;
The
a=33
is an expression, and when followed by a;
it is also an expression statement. An expression statement is only allowed where other statements are allowed; i.e. within a method or constructor declaration (in the body), or in an instance orstatic
initializer (block).Note that in the JLS, a method's body is part of the method's declaration. That is why we can say that statements are within a method declaration.
-
foo();
The
foo()
call is an expression, and when followed by a;
it is also an expression statement; see above.
It is a matter of understanding what is meant by "in" in this context. It means "inside of" rather than "directly inside of".
The imprecision arises when you consider that an expression statement that is inside of a method declaration that is inside of a class is ... also indirectly inside the class.
You could argue that the clause descriptive rather than normative since they don't specify precisely what "in" means in this context. But even so, the description is correct if you interpret it as above.
Your int a=23;
"example" is not an expression, so it is not apropos to the point you are trying to make. It could be a statement, or it could be a (package private) field declaration that is "in" a class or interface declaration.