java - static block inside static inner class -


we know static block in java resovled while compliling, not @ runtime. hence again know static inner class instantiated during first call nested class. suppose nested class having static block. so, in case can static block inside nested class resolved when first attempt access nested class made? sample code:

public class { public static class b {   static a;  static  {    a=new a();  }   public static geta() {    return a;  } } } 

now accessing as: a= a.b.getinstance(); hope @ point static block in b executed , not before that.

this should answer question:

public class test {      public test() {         system.out.println("test instantiated");     }      public static class inner {          static {             system.out.println("static block executed");         }          public inner() {             system.out.println("test.inner instantiated");         }      }  } 

when calling:

test test = new test(); test.inner inner = new test.inner(); 

we get:

test instantiated static block executed test.inner instantiated 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -