Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java just curly braces

Tags:

java

I was reading a book and there were a few example with programs that has just curly braces

for example

 public static void main(String args[]){
     //what is the uses of curly braces here.
     {
          //some code
     }
 }
like image 481
user373201 Avatar asked Dec 19 '10 20:12

user373201


People also ask

What is {} used for 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 do curly braces mean 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. \

How do you write curly braces in Java?

Java has a "feature" which allows you to omit curly brackets when writing if statements, for loops, or while loops containing only a single statement. You should never use this feature – always include curly brackets. The reason for this is because omitting curly brackets increases your chances of writing buggy code.

Do you need curly braces for if statements Java?

If the true or false clause of an if statement has only one statement, you do not need to use braces (also called "curly brackets"). This braceless style is dangerous, and most style guides recommend always using them.


3 Answers

It's a code block. The variables declared in there are not visible in the upper block (method body outside of these curlies), i.e. they have a more limited scope.

like image 50
Goran Jovic Avatar answered Sep 22 '22 05:09

Goran Jovic


Be careful, it is NOT ALWAYS an initialisation block as others have suggested. In your case it is a variable scoping mechanism called a Code Block or block.

If it is outside of a method, then it is!

Example

public class MyClass {

   {
      // this is an initialisation block
   }

}

However, if it is inside a method, it is NOT! In this case (which is the case in your example), it is a code block. Anything initialised inside the curly braces is not visible outside

Example

public static void main(String args[]){

     {
          String myString = "you can't see me!";
     }
     System.out.println(myString); // this will not compile because myString is not visible.
 }
like image 26
Codemwnci Avatar answered Sep 24 '22 05:09

Codemwnci


This idea of how to use curly braces as a coding construct is a debated issue in the Java world . There are several explanations people come up with when they see curly braces by themselves. So Im going to try to answer your question from a practical perspective.

The implied question in your post here is, really - when/why are these used ? Practically speaking, the following cases might result in a lone code block :

1) The programmer wanted additionally scoping to reuse variable names without fear of collisions for clarity (i.e. making several objects of the same type in a unit test or database insertion block).

other possible reasons :

2) Forgotten if/else/for/while loop code that is under development.

3) Remaining artifact of a removed if/else/for/while clause.

4) Autogenerated code uses scoping to simplify the creation of several similar components with identical variable names (i.e. consider a gui generator that needed to make code for 100 radio buttons - rather than incrementing variable names per button, it could use scoping).

5) As a tiny, reusable, pastable logical block with minimal side effects : the programmer felt like a block of code in a method was so obscure, its variables and internal side effects should have minimal visibility to the outside world. That is, the programmer has used a code block as a poor-man's anonymous lambda function (albeit, one without a return value). In this pattern one might do something akin to the below :

//lets say I want to make a primary key for a dogs name in a database. 
String dogNameKey=null;
{
    long time = System.currentTimeInMilliseconds();
    String theName = "spot";
    dogName=theName+"_"+time;
}

Clearly, the simple strategy for naming this record (dogNameKey) is not worthy of an external method - its too simple. But at the same time, the "time" variable should have no bearing or accessibility outside the logic for making this name up - i.e. it shouldn't even be relevant to the method which contains this tiny key generating block. So, by using braces, I've scoped it out . If a labmda were possible, than all of this scoping could be wrapped in a single, anonymous function.

Now - I could paste several of these blocks, and the variable names would be identical, so it would be easy to scan them by eye.

*Thus, when you see curly braces by themselves - they usually are pretty important - either they implement a specific custom-scoping, or they are an artifact of an error or potentially of autogenerated code. Scoping can also be used to "start" the refactoring of a method without actually writing a new method, by separating out its independant parts ... although IDEs are much better at this than humans. *

like image 38
jayunit100 Avatar answered Sep 23 '22 05:09

jayunit100