c*i 发帖数: 749 | 1 运行到哪一步装载class?
ClassA MyClass;
MyClass = new ClassA(); |
c*****t 发帖数: 1879 | 2
this statement only tells the compiler to reserve a variable space.
^^^^^^^^^^^^^^^^^^^^^^^
here.
【在 c*i 的大作中提到】![](/moin_static193/solenoid/img/up.png) : 运行到哪一步装载class? : ClassA MyClass; : MyClass = new ClassA();
|
xt 发帖数: 17532 | 3
I believe at run time, ClassA is loaded at this line.
【在 c*****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : this statement only tells the compiler to reserve a variable space. : ^^^^^^^^^^^^^^^^^^^^^^^ : here.
|
m******t 发帖数: 2416 | 4
I think so, too, because there is an implicit null assignment.
I wouldn't be surprised, though, if some VM chooses to optimizes by waiting
until next line, after all, it doesn't really _have to_ load ClassA on this
line.
It's actually fairly easy to find out, just take ClassA off the classpath, and
look at the exception stack trace. 8-)
【在 xt 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : I believe at run time, ClassA is loaded at this line.
|
g*******e 发帖数: 14 | 5
why? JVM does not require classA information for this line.
【在 m******t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : I think so, too, because there is an implicit null assignment. : I wouldn't be surprised, though, if some VM chooses to optimizes by waiting : until next line, after all, it doesn't really _have to_ load ClassA on this : line. : It's actually fairly easy to find out, just take ClassA off the classpath, and : look at the exception stack trace. 8-)
|
g*******e 发帖数: 14 | 6
I just try it. exception occurs in the next line.
【在 g*******e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : why? JVM does not require classA information for this line.
|
c*****s 发帖数: 214 | 7 非也。
可以做个小试验。在Class.forName里设个断点
ClassA myClass = null;
System.out.println(ClassA.class);
你会发现ClassA在第二行被加载。
另一个方法是在ClassA里加个static块,看看什么时候执行。
【在 xt 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : I believe at run time, ClassA is loaded at this line.
|
c*****t 发帖数: 1879 | 8 hehe, if you have done assembly for Java you won't say this :)
【在 xt 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : I believe at run time, ClassA is loaded at this line.
|
w******n 发帖数: 692 | 9 JVM spec doesn't require the lazy loading, though most JVM
works like this way.
【在 c*****t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : hehe, if you have done assembly for Java you won't say this :)
|
m******t 发帖数: 2416 | 10
waiting
this
classpath, and
Thanks, that's interesting to know.
【在 g*******e 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : I just try it. exception occurs in the next line.
|