Question

It's kind of hard to exactly search for this. I've seen this in a few places and I've even tried it in code to see how it works but just so that I know exactly how they can be used and to make sure I wont be abusing them I thought i'd ask on here.

 public void doSomethingSpecial()
 {

      String strHello = null;

      // What is the following section in braces for
      {
           strHello = "Hello World"
      }

      for(int i = 0; i < 10; i++)
      {

           //  What is the significance of them it in a loop?
           {
                strHello = "Hello";
                // Do something else...
           }
           .
           .
           .
      }
 }
  • Can someone clarify what these brace blocks braces are for?
  • I assume they work like a method and would restrict the scope of anything within as long as it isn't specified outside the block, but is there another use?
  • What is the exact name for them?

Thanks in advance

Was it helpful?

Solution

The braces define scope. For example, if you define a local variable within the braces and assign a value to it like this:

{
int foo = 10;

}

Then foo's scope (i.e. the section of code where it is define and has a value) is limited to the area within the braces.

The same goes for your for loop. Within those braces, the counter i is defined and has meaning, but not outside. So this code would not compile:

for (int i = 0; i < 5; i++) {
  // Do something with i
}

i = 10; // Won't compile. i is undefined as a variable.

The benefits of this are numerous in that you can precisely control the scope for different variables. You can also define the scope of things like exceptions in the context of exception handling, etc.

You can also do things like this:

int i = 0;

for (; i < 5; i++) {
  // Do something with i.
}

for (; i < 10; i++) {
  // Do something else with i
}

In this case i is defined in a larger scope space (that of the enclosing method or constructor) and the for loops are only incrementing its value. So you are sharing a variable among two different areas of scope.

Lastly you can use them outside of a method or constructor as initialization blocks in a class. They allow you to initialize member variables that, for example, cannot easily be initialized with a single line of code.

OTHER TIPS

Within the class, they form a static initializer, it runs once when the class is loaded, which happens first.

Within the method, they mark a scope. They are basically useless in your code.

But if the code is the following

  {
      String str = "test";
      System.out.println(str);
  }

  {
      System.out.println(str);
  }

The second print will report a compilation error, since the scope of str is in the first scope.

So found something I thought I would post on here for anyone as none of the other questions seem to exactly clarify this.

When used outside of a method in class scope:

 public class doSomethingSPecial() {
      {
           System.out.println("This is a constructor");
      }

      public doSomeThingSpecial() {}

      public doSomethingSpecial(String specialString) { }

      public doSomethingSpecial(int specialNumber) { }
 }

"The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors." - Java API Tutorial (Thanks to Soririous Delimanolis)

So the inline braces will be called each time a constructor is called.

As for the other questions regarding use within a method and loop the braces seem to be just to limit scope.

"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" - Credit to Robert Munteanu Top Answer: Anonymous code blocks in Java

That's basically everything I've come up with so far

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top