• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSL clarification regarding class initialization  RSS feed

 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(fixed post)

In JSL 3.0, chapter 12.4.1 "When Initialization Occurs" it says:

A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
# T is a class and an instance of T is created.
# T is a class and a static method declared by T is invoked.
# A static field declared by T is assigned.
-> # A static field declared by T is used and the field is not a constant variable (§4.12.4).
# T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

But what happens if the field is a constant variable?

A test* in the debugger shows, that it is initialized the same, when the field is used.

Can someone clear this up?

* for test I used this code with Sun JRE 1.6.0u17, in Eclipse v3.5.1:


Regards,
David
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Balažic wrote: * for test I used this code with Sun JRE 1.6.0u17, in Eclipse v3.5.1:


In your code, the field is a constant, but it is an instance of your class, so whenever it is accessed, your class needs to be initialized....
 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it must. Even when the field is of some other type.

Are you confusing "class initialization" with "object initialization"?

I changed line 3 of the code to :


Again, the class is initialized when line 11 in the first code snippet is executed. No matter if the field is final or not.

I understand that it is initialized in the case when the field is not a constant, because that is what the JLS says.

My question is: in the case when the field is a constant, where is it defined when the initialization happens?

I am writing a program and can not rely on accidental implementation behavior, but need confirmation in the specs.

Regards,
David
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!