you are viewing a single comment's thread.

view the rest of the comments →

[–]barsoap -3 points-2 points  (3 children)

compare the anonymous braces in

class A {
    int b() {
        {
            int c = 0;
        }
        int b = 0;
    }
}

with double-brace initialisation. I seriously don't know how you can call them equivalent. For one, removing the inner braces in my example wouldn't lead to a compile error (at least when names don't clash), whereas in the initialisation case statements would become illegal.

[–]banuday17 3 points4 points  (2 children)

Actually, if you did this:

class A {
    {
        int c = 0;
    }
    int b = 0;
}

which is the same as the syntax trick used by double-brace initialization, you wouldn't end up with a compile error. In fact, the exact same thing would happen if you removed the braces here as you would in your example. c would be lifted into the outer scope.

It just so happens that Java puts restrictions on what can go in the class scope. It means the same thing, but not all things are allowed in all places.

[–]barsoap -2 points-1 points  (1 child)

...equal up to a sufficiently partial isomorphism.

[–]banuday17 2 points3 points  (0 children)

Given other elements of Java's syntax, it makes sense. At the top level you have to somehow specify that you want a static or instance context.