2

I accidently made a pair of {} outside of all method and worked.

public static void main(String[] args) {
    System.out.println("ddd");
}

{
    System.out.println("ttt");
}

Of course if you run this code the result is "ddd" and it writes "ttt" only if I create a new instance of it.

And if I make it static {System.out.println("ttt");} it works as designed and the result is "ttt" then "ddd"

Is there any practical use of this? Why would anyone use it with contructor or without a written constructor?

My impressions are: it seems working, but smells like bad and strange practice. Am I right?

CsBalazsHungary
  • 793
  • 13
  • 29
  • Possible answer: http://stackoverflow.com/questions/12024095/java-empty-block-scope – Eric Smekens Apr 25 '13 at 10:04
  • This code is added to the constructor. Check this: http://stackoverflow.com/questions/5865069/why-is-this-java-code-in-curly-braces-outside-of-a-method – Pablo Lozano Apr 25 '13 at 10:05

7 Answers7

4

{} define the scope of a module or block of code (like a method, static block, class, etc.)

And every module should have a name of something to identify it from other modules.

In your case, simply putting {} means you are creating a block of code but not naming, hence it gives error. But putting {} inside a method will work fine.

But when you put static keyword before it, you are making a static block that has got special meaning in java. it means that everything inside static block will be executed when your class gets loaded first time.

See this link for initializer blocks from Java Tutorials website

Abubakkar
  • 15,090
  • 6
  • 52
  • 80
3

Is there any practical use of this?

There is one "idiom" that makes use of instance initializer blocks:

 Map mymap = new HashMap() {{put("a", 1); put("b", 2);}};

This is a concise way to create a map that is initialized with a given set of entries. And when you break it down, it is declaring and instantiating an anonymous subclass of HashMap which uses an instance initializer block to populate the new map.


My impressions are: it seems working, but smells like bad and strange practice.

That's a subjective statement. The only rational argument I can think of for initializer blocks being bad / strange is that people don't use them. And that argument smells of circular logic.

Stephen C
  • 669,072
  • 92
  • 771
  • 1,162
  • I agree, it wouldn't really smelly if everybody would get used to it. But the reason I feel it smelly is that the code is a lot more clear if you make a private method and call it in constructors. You might miss this {} thing when you review some code and it is somewhere far from the constructors. At least I wouldn't suspect at the first time that something is called outside of the constructor. and the obvious static things. But I see the practical use of {{ blahblah }} – CsBalazsHungary Apr 25 '13 at 10:48
2

Its all about Initializer blocks

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

{

// will execute when intialization

}

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

Suresh Atta
  • 118,038
  • 37
  • 189
  • 297
  • Yes, I just tried them, but is there any good practice use of it. For example, would you use it for anything? I would just use constructors and use private methods if it gets too fancy. – CsBalazsHungary Apr 25 '13 at 10:06
  • @CsBalazsHungary see updated answer.let me know still any lingering. – Suresh Atta Apr 25 '13 at 10:10
1

These are called initializer blocks. They are called along with all constructor. So any constructor call will invoke this code.

A static block is called only when class is loaded.

Lokesh
  • 7,830
  • 6
  • 43
  • 73
1

Normally, you have to put the 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:

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

Source: here

Linga
  • 10,077
  • 9
  • 48
  • 97
0

This is called initialization block in java

An initialization block is a block of code between braces that is executed before the object of the class is created. As the execution of the initialization block is dependent on the creation of the object we can easily make a guess that it has two types of object.

Mostly this is used for initialize some thing in your class which you want in initialize every time if you use any constructor.

Ex: Lets assume in your class you have 3 constructor.
1st one initialize IO.
2nd one initializing socket etc.
And you want to initialize some instance variable for your class, does't matter which constructor you are using. So for this you have 2 solution:

  1. Put initialization code in each constructor.
  2. Use initialization block and put those variable their and it will initialize those variable every time when any constructor call.
Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
  • 3. make a private method and do the things there. It seems some more normal solution, and you can see it is called, but it is subjective point of view. – CsBalazsHungary Apr 25 '13 at 10:49
0

It may be unfamiliar, but it is not bad per se. It's an initializer block that is executed on construction. See here for a more detailed explanation that includes static initialization blocks also.

hendalst
  • 2,747
  • 1
  • 19
  • 22