Redian新闻
>
超级菜鸟问题:什末时候load class
avatar
超级菜鸟问题:什末时候load class# Java - 爪哇娇娃
c*i
1
运行到哪一步装载class?
ClassA MyClass;
MyClass = new ClassA();
avatar
c*t
2

this statement only tells the compiler to reserve a variable space.
^^^^^^^^^^^^^^^^^^^^^^^
here.

【在 c*i 的大作中提到】
: 运行到哪一步装载class?
: ClassA MyClass;
: MyClass = new ClassA();

avatar
xt
3

I believe at run time, ClassA is loaded at this line.

【在 c*****t 的大作中提到】
:
: this statement only tells the compiler to reserve a variable space.
: ^^^^^^^^^^^^^^^^^^^^^^^
: here.

avatar
m*t
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 的大作中提到】
:
: I believe at run time, ClassA is loaded at this line.

avatar
g*e
5

why? JVM does not require classA information for this line.

【在 m******t 的大作中提到】
:
: 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-)

avatar
g*e
6

I just try it. exception occurs in the next line.

【在 g*******e 的大作中提到】
:
: why? JVM does not require classA information for this line.

avatar
c*s
7
非也。
可以做个小试验。在Class.forName里设个断点
ClassA myClass = null;
System.out.println(ClassA.class);
你会发现ClassA在第二行被加载。
另一个方法是在ClassA里加个static块,看看什么时候执行。

【在 xt 的大作中提到】
:
: I believe at run time, ClassA is loaded at this line.

avatar
c*t
8
hehe, if you have done assembly for Java you won't say this :)

【在 xt 的大作中提到】
:
: I believe at run time, ClassA is loaded at this line.

avatar
w*n
9
JVM spec doesn't require the lazy loading, though most JVM
works like this way.

【在 c*****t 的大作中提到】
: hehe, if you have done assembly for Java you won't say this :)
avatar
m*t
10

waiting
this
classpath, and
Thanks, that's interesting to know.

【在 g*******e 的大作中提到】
:
: I just try it. exception occurs in the next line.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。