-1

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.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • What specifically are you referring to with regards to the style? – BVSmallman Dec 06 '11 at 01:22
  • Why is there a block of code under `call2()`? – Paul Dec 06 '11 at 01:22
  • Uh, what in particular do you noticed about this "style"? The curly brace placement? Lack of camelcase names? Something else entirely? Please clarify just what you're looking for. – paulsm4 Dec 06 '11 at 01:23
  • 1
    possible duplicate of [Anonymous code blocks in Java](http://stackoverflow.com/questions/1563030/anonymous-code-blocks-in-java) – paulsm4 Dec 06 '11 at 01:26

8 Answers8

5

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.

yshavit
  • 42,327
  • 7
  • 87
  • 124
4

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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
2

It's an anonymous code block; see Anonymous code blocks in Java for some discussion of what it is (and is not) useful for.

Community
  • 1
  • 1
ruakh
  • 175,680
  • 26
  • 273
  • 307
0

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    While that particular _example_ is unnecessary, the idea itself is sometimes useful to restrict the duration/visibility of variables. – paxdiablo Dec 06 '11 at 01:25
  • @paxdiablo Agreed, but still, almost never necessary. – Dave Newton Dec 06 '11 at 01:27
  • 1
    Re: almost never. I've seen lots of code where there are "temporary" variables that get assigned all over again in different, unrelated places. Locally scoped separate variable would have been better. – Thilo Dec 06 '11 at 01:31
  • @Thilo Cool. Tangential, though, and likely in need of refactoring to eliminate multiple problems at once. – Dave Newton Dec 06 '11 at 01:34
  • 1
    Yes, breaking the code into several private methods is probably a good idea in most of these cases. But sometimes that won't work well because the blocks need a lot of "context" in the form of other local variables (that they do share). – Thilo Dec 06 '11 at 01:38
0

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).

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

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.

Mike Daniels
  • 8,582
  • 2
  • 31
  • 44
0

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);
  }
Thilo
  • 257,207
  • 101
  • 511
  • 656