Anonymous code blocks in Java
https://stackoverflow.com/questions/1563030/anonymous-code-blocks-in-java
Question:
Are there any practical uses of anonymous code blocks in Java?
public static void main(String[] args) {
// in
{
// out
}
}
Please note that this is not about named blocks, i.e
name: {
if ( /* something */ )
break name;
}
Answer:
They restrict variable scope.
public void foo()
{
{
int i = 10;
}
System.out.println(i); // Won't compile.
}
In practice, though, if you find yourself using such a code block that's probably a sign that you want to refactor that block out to a method.