3 ms·
The first code sample in the article is wrong: > In most curly-braced languages, blocks delineate lexical scope. For example, in C or Java: { int
by finnw 15y ago
The first code sample in the article is wrong:
> In most curly-braced languages, blocks delineate lexical scope. For example, in C or Java:
{
int i = 13 ;
{
int i = 42 ;
print(i) ;
}
print(i) ;
}
In fact this will not compile in Java.
- ufo 15y agoI am not very familiar with Java details. Can you explain why this doesn't compile?
- finnw 15y agoIt's this part: int i; { int i; } Java does not allow you to shadow one local variable with another. This is ok: { int i; } int i; because the scopes do not overlap. This is ok too: int i; { class LocalClass { public void method() { int i; } } }