Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


I am not very familiar with Java details. Can you explain why this doesn't compile?


It'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;
            }
        }
    }




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: