wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initialization 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 "Initialization " Watch "Initialization " New topic


Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
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.

Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
are you sure this is the whole code? seems to me like somethings are missing

Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
According to JLS 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
Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
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
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Relax, so you get 96% instead of 100%, who cares?

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
Yeah right...i wish actually... but who knows i might surprise myself... man am i freaked out or what..
ok so barry how about explaining the above code..
Mallik Hiremath
Ranch Hand

Joined: Oct 20, 2002
Posts: 46
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
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
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
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
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.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
This is the bug
Saniya Ansari
Ranch Hand

Joined: Sep 30, 2002
Posts: 48
ok thanks people.. i think i get it..
I agree. Here's the link: http://aspose.com/file-tools
subject: Initialization
Similar Threads
About Block Initializer
Something weird in JLS (or in JBuilder)
Forward reference using class name
forward reference
Forward Referencing