File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 agree. Here's the link:
subject: Initializer expression execution
It's not a secret anymore!