As per JLS section 12.4.1. When Initialization Occurs
A class or interface 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.
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
Dalvir Singh Bains wrote:since all the enum values internally are public , static and final constants, so as per the fourth point mentioned above using the enum field NOKIA for the first time shouldn't trigger the execution of static block in enum Phone during the class loading of Demo2 by JVM.
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.29). Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1), reachability (§14.22), and definite assignment (§16.1.1).
And a static field used as a public constant, which is indeed a constant expression (sometimes called, “a compile‑time constant”), would cause the class to be initialised similarly because it would be used by the print statement.Mike Simmons wrote:. . . using an enum constant for the first time can, indeed, trigger class initialization.
Aaaaaaaaaaaaaaaaaaaaaaaaah!Mike Simmons wrote:. . . . If it had been a constant variable . . . it would not trigger class initialization. . . . .
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
Mike Simmons wrote:If only there were some place where they documented what Class.forName() did, they might tell us clearly whether or not it triggers initialization. If only…
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
javadoc wrote:
* @throws ExceptionInInitializerError if the initialization provoked
* by this method fails
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
That goes with the old usage of Class.forName() to set up a database connection; the class has a static initialiser which must run when the class is initialised.Tim Holloway wrote:. . . Initialisation must have been done before this method can successfully return!
Tim Holloway wrote:Oh, wait:
javadoc wrote:
* @throws ExceptionInInitializerError if the initialization provoked
* by this method fails
So yes, there's your answer. Initialisation must have been done before this method can successfully return!
Java 23 docs wrote:A call to forName("X") causes the class named X to be initialized.
Yeah, you might want to avoid initialization if the class in question used a lot of resources in the process of initialization, but you wanted be assured it was then for when you needed it.Mike Simmons wrote:Thank you, Paul C.
Dalvir has shown how this can be easily tested, as well. Note that the API also documents a way to avoid initialization, if desired. The ExceptionInInitializer text doesn’t show that it must happen, only that it may happen.
I don’t know why they were so obsessed with this, but they did document it carefully from the very beginning. I think they were concerned with “write once, run anywhere” and didn’t want different platforms to behave differently.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
What are you doing in my house? Get 'em tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|