重新开始再看一遍Thinking In Java,今天发现一点小问题.
Chapter 4
They(static variable) are initialized only when the first Table object is created (or the
first static access occurs)
其实这个描述应该不是非常的正确,比如我用Table.class,没有用到上面描述的两种情况,也会让类被加载,从而初始化静态变量,更准确地说是应该类被加载的时候初始化static的变量或者block。
结合这个贴子:http://www.iteye.com/topic/208187
里面父类合子类的初始化顺序,如果理解了"static
initialization takes place only once, as the Class object is loaded for
the first time"就能很清楚的理解原因了.- -!
Chapter 4
They(static variable) are initialized only when the first Table object is created (or the
first static access occurs)
其实这个描述应该不是非常的正确,比如我用Table.class,没有用到上面描述的两种情况,也会让类被加载,从而初始化静态变量,更准确地说是应该类被加载的时候初始化static的变量或者block。
结合这个贴子:http://www.iteye.com/topic/208187
里面父类合子类的初始化顺序,如果理解了"static
initialization takes place only once, as the Class object is loaded for
the first time"就能很清楚的理解原因了.- -!
本文讨论了Java中静态变量的初始化时机,并指出它们并非仅在创建第一个对象或首次静态访问时初始化。通过具体示例说明了类加载过程同样会触发静态变量的初始化。
3003

被折叠的 条评论
为什么被折叠?



