L
Lew
Wrong. Loading and initialization go hand-in-hand.
What?
See JLS 12.4, which I cited upthread:
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4>
"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 (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.
A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.
Invocation of certain reflective methods in class Class and in package java..lang.reflect also causes class or interface initialization.
A class or interface will not be initialized under any other circumstance."
Wrong. Loading and initialization go hand-in-hand.
Not according to the JLS. What authority are you using?
You haven't cited anything except my own post, and your quotation of
*that* was a jumbled mess with some sections repeated for some reason.
Again, I quoted the section I re-quoted in this post. I provided the link
*and* quoted the relevant content. How can you say I didn't?
On your part, Lew.
Wrong. Loading and initialization go hand-in-hand.
Except according to the JLS.
And not before.
When invoking a static method of a non-loaded class, the standard
procedure always has been:
Load the class.
Initialize the class.
Note - that is two steps, not one.
Invoke the method.
In particular, the spec requires that in this:
class A {
static int x;
static { x = 100; }
static int foo () { return x; }
}
a call to A.foo() should return 100, not 0. If it can attempt to invoke
foo before the static initializer has executed that would be violated.
It can attempt to call 'foo()' before the initializer has executed. This is
exactly one of the circumstances that causes the initializers to execute.
Read the JLS! I have referenced and quoted the relevant section twice now.
On the other hand, it complaining that a reflective call to A.bar()
can't be resolved sooner than the loading and initializing of A as part
of trying to resolve bar is equally anomalous. If it hasn't loaded and
I haven't seen any examples of reflection in this thread. Regardless,
"certain reflective methods" invoked will cause initialization.