This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

JSL clarification regarding class initialization  RSS feed

 
Ranch Hand
Posts: 98
  • 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
 
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: 98
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!