5

I recently read some code that uses a special syntax regarding {}, I've asked a more experienced Java developer, but he also can't answer.

public void doSomething() {
    someWorks();
    {
        someVariables;
        someMoreWorks();
    }
    someEvenWorks();
    {
        ...
    }
}

Why does the code author put these lines inside {}? I guess that the variables declared within the {} will be released right after execution exits {}, right, because I can't access these outside the {} anymore?

Cajunluke
  • 3,103
  • 28
  • 28
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • 4
    You're right - it has to do with limiting scope. – duffymo Sep 07 '11 at 13:48
  • 3
    I think this is a duplicate of [this question](http://stackoverflow.com/questions/5466974/multiple-open-and-close-curly-brackets-inside-method-java) – momo Sep 07 '11 at 13:54

3 Answers3

6

Yes, the only difference is for scoping.

Occasionally this can be useful for throwaway code such as micro-benchmarks where you want to be able to cut and paste a block and make a minor change, then potentially reorder the blocks.

I would rarely (if ever) have something like this in "real" code though.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I use this sometimes in "real" code to limit the scope of variables, but it kind of has the bad smell of a method that is too long. – Thilo Sep 07 '11 at 13:54
  • I think, it maybe very useful! For example, you need temporary variable to save some value, you may put them into {}, then later you can use that name again (with different data type). Or, if the function is a long work, GC may collect the temporary variables inside {}. – Luke Vo Sep 07 '11 at 14:01
  • 2
    @W.N. I'm not sure whether it affects GC - but if I have a method that long, I'd prefer to refactor. – Jon Skeet Sep 07 '11 at 14:03
  • @DatVM you don't need {} to remove unused variables. Garbage Collector does it. but it may be useful for reusing the variable name :) – Celik Aug 11 '14 at 15:06
  • @JonSkeet Wouldn't you ever use them to initialize anonymous classes? – Haggra May 27 '16 at 07:51
  • @Haggra: That's not the same thing - that would be an instance initializer block, which is different from introducing a block within a method. – Jon Skeet May 27 '16 at 07:57
4

This gives him a nested scope to declare "more local" variables.

I guess that the variables declared within the {} will be released right after exit {}, right, because I can't access these outside the {} anymore?

Depends on your definition of "release" (they will most likely not be garbage collected until the method ends, so if this is important, you might want to null them out), but yes.

Other rarely seen uses of curly brackets include class and instance initializers:

class A {
   static {
      // some class initialization code
   }

   {
      // some instance initialization code
   }

 }
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • So, that means the memory used by those variables isn't freed after exit the {} block? – Luke Vo Sep 07 '11 at 13:51
  • I am not sure. See this question: http://stackoverflow.com/questions/473685/does-it-help-gc-to-null-local-variables-in-java – Thilo Sep 07 '11 at 13:57
1

The fact that the author put those variables in {} indicates the scope of those variables will only be that of the method defined by the {}; in turn, those variables will be up for garbage collection once method finishes execution.

amickj
  • 138
  • 2
  • 10