开发者

Java - Braces after variable definition?

I was going t开发者_高级运维hrough some Java tutorials in a book and ran across code like this:

private void theMethod( MyObject table )
{
  SomeObject item1 = table.add(0,0,0,"Item 1");
  {
    item1.setWhatever = false;
    item1.setSomething = 15;
  }

  // More code here...
}

What is the purpose of the braces following the variable definition?


I use this braces sometimes in group when I'm using a lot of local variables. This way, they are out of scope and I can create variables with the same name.

I can't find immediately a good example, but I mean something like this:

/* Code block 0 */
{
    int localStuff = 9;
    boolean submit = false;
    // Do some IO, or whatever
}
/* Code block 1 */
{
    int localStuff = 4;
    boolean submit = false;
    // Do some IO, or whatever
}

/* Code block 2 */
{
    int localStuff = -1;
    boolean submit = true;
    // Do some IO, or whatever
}


In this particular example, it doesn't do anything (maybe just improving the code's look, based on the coders' taste). But, it could be also a typo :)

BTW, braces can be helpful for limiting the scope:

private void theMethod( MyObject table )
{
  {
    SomeObject item1 = table.add(0,0,0,"Item 1");
    item1.setWhatever = false;
    item1.setSomething = 15;
  }
  // Item1 is not defined here anymore, so for example you can define another object    with the name item1
  // Though it's not a good practice.
  OtherClass item1;
  // More code here...
}


Are you sure it was like that? Although code blocks can be defined like that, it doesn't do much, except limiting the scope of local variables, which isn't exciting.

However, class-level anonymous code blocks are useful to initialise variables, and populate data structures, etc.

For example, this static anonymous block initialises a final static variable.

public class Snippet {

    private final static Map config;
        static {
            HashMap tmp = new HashMap();
            tmp.put("moo","foo");
            tmp.put("bar","baz");
            config = Collections.unmodifiableMap(tmp);
           }
    // rest of class here.
    }

It works int the same way for non-static blocks and instance variables, which is less useful, but handy if you want to make sure that all constructors and subclasses executes the same code.

I found this thread, which is not a duplicate, but related. Anonymous code blocks in Java


That's normal, the block is used just to organize the code, it's evaluated as a normal flow

class Foo{
    private void theMethod(String str )
    {
      {
        System.out.println(str);
        {}{}{}{}{}{}{}{}
      }
    }

    public static void main(String ...args){

        new Foo().theMethod("my string");
    }
}

Similar way with ;

class Foo{
    private void theMethod(String str )
    {
      {
        System.out.println(str);
        {;}{;}{;}{;}{;}{;}{;}{;}
      }
    }

    public static void main(String ...args){

        new Foo().theMethod("my string");;;;;;;;;;;;;;;;;
    }


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜