This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializer expression execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializer expression execution" Watch "Initializer expression execution" New topic
Author

Initializer expression execution

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Oct 12, 2000
Posts: 5093
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.


42
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Initializer expression execution
 
Similar Threads
Interface Q
Inner Classes
initialization sequence
explain this ...please
Why are finals allowed for method-local classes??