java - Commands in anonymous blocks inside a class of any use -


if seen question here: instance initializer , *this* keyword

and ask myself this:

public class main {      public static void main(string args[]) {             new main();     }      { system.out.println(x); } //error here      int x=1; } 

of (even theoritical) use? mean part:

 { system.out.println(x); } //error here 

as far can anonymous i'd have no idea how execute manually, doesn't seem executed automatically not part of function or whatsoever. sorry if questions answered, ones found targeted {} restrict variable scope in case not think of way scope or make run.

it's instance initialization block. whenever create instance of class, executed before body of constructor use.

therefore way execute manually creating instance of class in block appears (or sub-class of class).


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -