• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables and forward reference

 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the code snippet above, how come X could be referred in the static block, even though it is only declared afterwards? I thought all static code is executed sequentially from top to bottom. How does the compiler know about X at //1 ?

Thanks.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can forward reference variables to assign value to them. You cannot forward reference to read their values. So your code is legal but this code would not be legal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic