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
  • 803
  • 14
  • 30
  • 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

6 Answers6

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,488
  • 8
  • 55
  • 83
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
  • 698,415
  • 94
  • 811
  • 1,216
  • 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
  • 120,458
  • 37
  • 198
  • 307
  • 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,810
  • 6
  • 48
  • 78
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,379
  • 10
  • 52
  • 104
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,957
  • 1
  • 24
  • 25