I'm looking at this code and it looks like:
blah("....");
{
call1(blah);
call2();
{
inner1("...");
inner2("...");
}
}
I'm new to java so interested to know what to search, this style looks interesting.
I'm looking at this code and it looks like:
blah("....");
{
call1(blah);
call2();
{
inner1("...");
inner2("...");
}
}
I'm new to java so interested to know what to search, this style looks interesting.
It's not doing things inline, it's just setting up a separate scope. If you declare variables in that "inner" block, they won't be available in the outer one. It can be useful if you copy-paste a few lines of code and want to make sure you aren't reusing variables from the first bit in the second bit. I've done it occasionally in unit tests, but more often than not, it's a sign that you may need to refactor a bit.
This is just a local scope. You can introduce scopes at heart's content.
void foo()
{
int a;
{
int b = get();
bar(a, b);
}
{
int b = get();
bar(a, b);
}
}
Each variable is only visible inside its scope, so you can use this to keep the namespace clean. In the above example, without the local scopes, I would have to have one declaration of b
and then repeated assignments. Thanks to the distinct scopes, I can just copy/paste the entire block, and I never leak the name b
into the larger scope so I don't have to worry about it later.
It's an anonymous code block; see Anonymous code blocks in Java for some discussion of what it is (and is not) useful for.
It's just a block containing a group of statements, nothing really special although it is sometimes useful to limit the scope of a variable(a). You can do exactly the same thing in C such as:
int main (void) {
int i;
{
i = 1;
}
return 0;
}
(a): Variables declared inside such a block are not visible outside that block, so something like:
{
int i = 1;
}
System.out.println (i);
would not be valid since the i
"disappears" at the closing brace.
It's called "using unnecessary blocks".
The only benefit would be to ensure no variables are shaded.
IMO it's almost always noise, and see it rarely. (Read: essentially never, over millions of lines of code over 12+ years. When I do it's in immature code.)
I wouldn't recommend it as a general thing.
That's a statement block. See the Java Language Specification section 14.2. In the code you posted, it appears useless. It's useful to establish a local scope for a variable (among other things).
That's just a block
, a bunch of statements enclosed in curly braces. You're probably used to seeing them as the body of an if
statement, but maybe you never thought of them that way.
if (foo) { bar; }
I haven't seen much code written with extra blocks, although I have sometimes wondered if it would be useful to use them to denote sections of (for example) particularly long, verbose UI code. I have seen them occasionally used in OpenGL code to make glBegin and glEnd statements stand out.
glBegin(GL_LINES);
{
.. draw a bunch of primitives ..
}
glEnd();
There may also be rare circumstances where you want to use them to limit the scope of some variable, since variables are only scoped to the block where they were declared.
It makes no sense in your example (except maybe logical grouping), but you can use these blocks to get "more local" variables (so that you can reuse the names):
{
String x = "zzzz";
String y = doSomething(x);
doSomethingElse(y);
}
{
int x = 12;
long y = foo(x);
bar(y);
}