java - What is the purpose of the {} outside of any methods? -


this question has answer here:

i accidently made pair of {} outside of method , worked.

public static void main(string[] args) {     system.out.println("ddd"); }  {     system.out.println("ttt"); } 

of course if run code result "ddd" , writes "ttt" if create new instance of it.

and if make static {system.out.println("ttt");} works designed , result "ttt" "ddd"

is there practical use of this? why use contructor or without written constructor?

my impressions are: seems working, smells bad , strange practice. right?

is there practical use of this?

there 1 "idiom" makes use of instance initializer blocks:

 map mymap = new hashmap() {{put("a", 1); put("b", 2);}}; 

this concise way create map initialized given set of entries. , when break down, declaring , instantiating anonymous subclass of hashmap uses instance initializer block populate new map.


my impressions are: seems working, smells bad , strange practice.

that's subjective statement. rational argument can think of initializer blocks being bad / strange people don't use them. , argument smells of circular logic.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -