java - What is the purpose of the {} outside of any methods? -
this question has answer here:
- why java code in curly braces ({}) outside of method? 3 answers
- java empty block scope 3 answers
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.