java - What is RIWO (Read Indirectly Write Out) state -


i reading static flow control , came across riwo concept. can explain simple terminology , perhaps code sample?

this related error "illegal forward reference".

relevant link.

after going through material , discussing couple of guys offline found out following information.

when java class getting executed there few steps jvm performs few steps sequentially.

  • identify static members top bottom.
  • executes static variables assignments , static blocks top bottom.
  • executes main method.

during these phases there 1 such state called riwo(read indirectly write only) static variable.

during riwo variable cannot accessed directly reference. instead need use indirect way call variables.

for example:

class riwo {  static int = 10;  static  {   system.out.println(i);  }  } 

in above case output 10.

class riwo { static int = 10; static {     m1();     system.out.println("block1"); }  public static void main(string... args) {     m1();     system.out.println("block main"); }  public static void m1() {     system.out.println(j);     system.out.println("block m1"); }  static int j = 20; static {     system.out.println("end of code"); } } 

in above case output

0 block m1 block1 end of code 20 block m1 block main

class riwo { static {     system.out.println(i);     system.out.println("block1"); } static int = 10; public static void main(string... args) {     system.out.println("main block"); } } 

in above case following compile time error

riwo.java:5: illegal forward reference system.out.println(i);

that means cannot read static variable directly when in riwo state.we should call variable indirectly using method.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -