• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static final usage

 
babu thannasi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all , i am preparing for OCAJP 7 , i was reading thru one of the posts related to Static in this forum, and used the code from that , while executing below code.



code prints --> inside Static : int 1 10 int2 0 and inside main: int 1 10 int2 2.

how ever when i removed the final , i see a inside Static : int 1 0 int2 0 and inside main: int 1 10 int2 2.

So the Question is , what is happening here with and with out final( on line 6 & 7) , could any one please help me understand why it is so ?
 
Paweł Baczyński
Bartender
Posts: 1877
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is, because final fields are initialized before any other:
JLS SE7 wrote:The procedure for initializing [class or interface] C is then as follows:

(...)

6. (...) Then, initialize each final static field of C with the constant value in its ConstantValue attribute (§4.7.2), in the order the fields appear in the ClassFile structure.

(...)

9. Next, execute the class or interface initialization method of C.

(...)


Non-final statics (variables and static blocks) are executed in "interface initialization method" (this is a special method named <clinit> generated by the compiler) in order they appear in a source file.
So, your static block is executed before variables (now non-final) have their value assigned.

When they were final they had their value assigned before static block was executed.

Try moving your static block below variable declarations. You will see that whether variables are declared final has no affect on the output.
 
babukumar thannasi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Pawel, i tried moving the static block below variable declarations, i see the response as explained by you. could you please share the link to JLSSE7 or the section in which this final initialization is define?

i am searching in this page https://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2 , could not find the steps you mentioned.
 
Paweł Baczyński
Bartender
Posts: 1877
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry! My mistake. This quote comes from JVMS, not JLS.

Here is the link:
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html#jvms-5.5
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
babukumar thannasi wrote:thanks Pawel, i tried moving the static block below variable declarations, i see the response as explained by you. could you please share the link to JLSSE7 or the section in which this final initialization is define?

i am searching in this page https://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4.2 , could not find the steps you mentioned.


Pawel actually kept the numbers when he cut out everything else (that was not relevant). You can't see step 6 and step 9 that was mentioned?


Anyway, to elaborate a bit, the "final" part of step 6 is really part of the compile time constant requirement. This, of course, is used for an optimization -- as there is no need to initialize a field at runtime when it can be calculated at compile time.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Oh, sorry! My mistake. This quote comes from JVMS, not JLS.


Interestingly, while the specs are different, it is very similar, and even matched to step 6 and step 9. It is only the wording that is different. I will speculate that the two sections was cut and pasted from each other.

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
babu thannasi wrote:So the Question is , what is happening here with and with out final( on line 6 & 7) , could any one please help me understand why it is so ?

This thread provides very good explanations (including a bunch of code examples) about the same topic. So definitely have a read, will be very informative!
 
babu thannasi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Paweł Baczyński wrote:Oh, sorry! My mistake. This quote comes from JVMS, not JLS.


Interestingly, while the specs are different, it is very similar, and even matched to step 6 and step 9. It is only the wording that is different. I will speculate that the two sections was cut and pasted from each other.

Henry


thanks Henry, thats what i thought after reading both the pages.
 
babu thannasi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
babu thannasi wrote:So the Question is , what is happening here with and with out final( on line 6 & 7) , could any one please help me understand why it is so ?

This thread provides very good explanations (including a bunch of code examples) about the same topic. So definitely have a read, will be very informative!


thanks Roel , execellent thread!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic