java - Curly braces only method -


this question has answer here:

why java accept brackets only method? made for?

{     // } 

i noticed executed automatically after static-block before constructor. although constructor of super class executed before.

is there specific reason order?

this junit made discovering execution order:

public class testclass extends testsuperclass {      public testclass() {         system.out.println("constructor");     }      @test     public void test() {         system.out.println("test");     }      {         system.out.println("brackets");     }      static {         system.out.println("static");     } } 

public class testsuperclass {      public testsuperclass() {         system.out.println("super class constructor");     }      {         system.out.println("super class brackets");     }      static {         system.out.println("super class static");     } } 

as output get:

super class static static super class brackets super class constructor brackets constructor test 

it same static block not static. execute in order found out - after super constructor before constructor. however, if static-block can useful regular block not much. never used, legal. never saw used , can not think of particular purpose, part of valid syntax. if not static block wouldn't either.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -