Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializer expression execution

 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look at the code below:

It compiles fine, now instead of "this.i" i replace with i, and instead of Initializers1.k i have k,
It gives compiler errors in the lines.
This code would give compiler errors:


Please let me know any suitable explanation for such behaviour
Thanks
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static members are resolved (and thus initialised) in the order they are declared.
As you're declaring k only after its first use in the initialisation of z you're getting an error.
In the first case you're getting a forward reference which is resolved at runtime, in the second it's (attempted to be) resolved at compile time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic