This is about initialization.. I was going through JLS and they gave an example (this is just a snippet from the code..you can refer to JLS chap8) . I dont understand why the first declaration of x=100; is ok becoz x hasn't been declared yet.
According to JLS 188.8.131.52 the assignment should be allowed because it does not comply with the second point: " The usage is not on the left hand side of an assignment." However there is a compile error. In fact this a known bug in JLS.
SCJP2. Please Indent your code using UBB Code
Joined: Sep 30, 2002
ok the whole code is this...
i think i missed the static int x; towards the end.. but does that mean all static variables are initialized as soon as they are loaded and forward referencing does not apply here..?? ok my exam is tomorrow.. PANICKKKK
Okay here is my 2 cents, We can assigne values (that must be evaluated at compile time and that expression should not making use of (extract) any values from the un declared variables) for example when we write int n=j=200 we are assigning value 200 to n and as well as j, its not that we are assigning the value of j to n . the bottom line is you can not extract value of a variable until you declare it Thank you
Hi Saniya, The code above won't compile because of an illegal forward reference of x and j. A static block cannot make a forward reference to static variables that are defined after its definition. This applies also to Instance blocks and instance variables.
Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Maybe the following will help. - class is loaded (& verifed, linked, etc. etc.) - memory is allocated for the static variables. - static initializers are executed - Memory is allocated for the object - memory for all the attributes of the class - the attributes are given default values - instance initializers are then executed. (the instance initializer blocks are moved into the constructor after the super() or this() call) If there are any super classes, then the above steps are repeated recursively until class Object. Read the JVM specifications if you want to understand the class file structure - very interesting. I'm sure I have missed something in the above steps - but you get the gist. The bartenders would neway correct me. You seem to be a different time zone - If i'm not too late, All the best. Relax.