This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializer expression execution" Watch "Initializer expression execution" New topic

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
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.

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Initializer expression execution
It's not a secret anymore!