Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialization

 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you sure this is the whole code? seems to me like somethings are missing
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to JLS 8.3.2.3 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.
 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Relax, so you get 96% instead of 100%, who cares?
 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vin Kris
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the bug
 
Saniya Ansari
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks people.. i think i get it..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic