Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple open and close curly brackets inside method. - Java

Tags:

 public class MyTestClass {      public static void main(String[] args) {         new MyTestClass().myMethod();     }      public void myMethod(){         {         //do something          }         {             //do something          }         {             //do something          }     }//method close  }//class close 

What is the benefit of doing this? I have seen this kind of code.

like image 711
B. S. Rawat Avatar asked Mar 29 '11 01:03

B. S. Rawat


People also ask

What is the purpose of {} squiggly braces in Java?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What is the meaning of {} in Java?

In Java when you open a curly brace it means that you open a new scope (usually it's a nested scope). One important thing to understand is that a variable that you'll define inside this scope (which end where you put the closing curly brace) will not be recognized outside of the scope.

How do you replace open curly braces in Java?

Summary. If one needs to use the replace() function to replace any open curly brackets "{" contained within a String with another character, you need to first escape with a backslash "\". This syntax not only applies to curly brackets { }, but also with "square brackets" [ ] and parentheses ( ).

How do you use curly brackets in Java?

In a Java program, everything is subordinate to the top line — the line with class in it. To indicate that everything else in the code is subordinate to this class line, you use curly braces. Everything else in the code goes inside these curly braces. In an outline, some stuff is subordinate to a capital letter A item.


2 Answers

It is not common practice to do this kind of thing, and I wouldn't do it normally.

They are defined as Blocks in the JLS, here.

Those inner blocks ( i.e. { ... } ) can serve a couple of purposes:

  • Blocks limit the scope of any variables declared within them; e.g.

    public void foo() {     int i = 1;     {          int j = 2;     }     // Can't refer to the "j" declared here.  But can declare a new one.     int j = 3; } 

    However, I wouldn't recommend doing this. IMO, it's better to use different variable names OR refactor the code into smaller methods. Either way, most Java programmers would regard the { and } as annoying visual clutter.

  • Blocks can be used to attach labels.

    HERE : {     ...     break HERE;  // breaks to the statement following the block     ... } 

    However, in practice you hardly ever see labelled break statements. And because they are so unusual, they tend to render the code less readable.

like image 55
Stephen C Avatar answered Sep 18 '22 21:09

Stephen C


public void stuff() {   int i = 48;    {      int i = 21;     System.out.println(i); // prints 21   }   System.out.println(i); // prints 48 } 

Basically, it's a way to create scopes smaller than entire function... Benefit?.. have the people stare at your code longer before they understand it... IMO it's bad style and should be avoided

like image 23
iluxa Avatar answered Sep 17 '22 21:09

iluxa