Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this Java code in curly braces ({}) outside of a method?

I am getting ready for a java certification exam and I have seen code LIKE this in one of the practice tests:

class Foo {       int x = 1;       public static void main(String [] args) {           int x = 2;           Foo f = new Foo();           f.whatever();       }       { x += x; }  // <-- what's up with this?     void whatever() {           ++x;           System.out.println(x);       }   } 

My question is ... Is it valid to write code in curly braces outside a method? What are the effects of these (if any)?

like image 480
nairdaen Avatar asked May 03 '11 04:05

nairdaen


People also ask

What is the purpose of {} in Java?

It's called an initializer block and is invoked every time an instance of the class is created. The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

What do curly brackets mean 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 remove curly braces from string in Java?

String n = s. replaceAll("/{", " "); String n = s. replaceAll("'{'", " ");

How do you replace open curly braces in Java?

String h = "{hiren:}"; h=h. replaceAll(":\\}", ":\"\"}"); Otherwise, you can use String#replace with no regular expression nor escaping needed. String h = "{hiren:}"; h=h.

What do curly braces do in Java?

What Do Curly Braces Do in Java? Curly brackets or braces in Java are used to delimit blocks of code inside a program. That is it determines the limits or boundaries of the code. They are most commonly used with structured control flow constructs such as if/else, while/for loops, and subroutines (methods).

What is a curly brace language?

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 Source Code and What Does it Do?

What are curly brackets in C++?

Curly braces play a big role in code structure within popular programming languages such as Java, C++ and more. Here's how to properly line up code with curly brackets. At their core, all programming languages share similarities.

Should curly brackets go on the same line or different lines?

However, experienced developers prefer a 'same-line' approach, which means the curly bracket goes on the same line of code that initiates the new code block. Popular Java IDEs such as Eclipse, NetBeans and IntelliJ use this same-line approach as the default setting for code formatters.


1 Answers

Borrowed from here -

Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods. Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

{     // whatever code is needed for initialization goes here }  

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

You may also wanna look at the discussions here.

like image 120
Shankar Raju Avatar answered Oct 14 '22 03:10

Shankar Raju