类的静态块什么时候执行?
我有 2 个 jar,我们称它们为 a.jar 和 b.jar。
b.jar 依赖于 a.jar。
在a.jar中,我定义了一个类,我们称之为StaticClass。在StaticClass中,我定义了一个静态块,调用一个名为“init”的方法:
public class StaticClass {
static {
init();
}
public void static init () {
// do some initialization here
}
}
在b.jar中,我有一个main,因此在main中,我期望已调用init()方法,但实际上没有。我怀疑这是因为 StaticClass 尚未被 jvm 加载,有人能告诉我
- 我的结论是否正确?
- 什么触发jvm加载类?
- 如何让静态块自动执行?
谢谢
I have 2 jars, let's call them a.jar and b.jar.
b.jar depends on a.jar.
In a.jar, I defined a class, let's call it StaticClass. In the StaticClass, I defined a static block, calling a method named "init" :
public class StaticClass {
static {
init();
}
public void static init () {
// do some initialization here
}
}
in b.jar, I have a main, so in the main, I expect that the init() method has been called, but actually not. I suspect that is because the StaticClass has not been loaded by the jvm, could anyone tell me
- Is my conclusion correct?
- What triggers the jvm to load a class?
- How can I get the static block executed automatically?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
是的你是对的。静态初始化块在 JVM(具体来说是类加载器)加载
StaticClass
(在代码中第一次引用它时发生)时运行。您可以通过显式调用
StaticClass.init()
强制调用此方法,这比依赖 JVM 更好。您还可以尝试使用 Class.forName(String) 强制 JVM 加载该类并调用其静态块。
Yes, you are right. Static initialization blocks are run when the JVM (class loader - to be specific) loads
StaticClass
(which occurs the first time it is referenced in code).You could force this method to be invoked by explicitly calling
StaticClass.init()
which is preferable to relying on the JVM.You could also try using
Class.forName(String)
to force the JVM to load the class and invoke its static blocks.首先,类加载与类初始化不同。对于任何从 Java 语言规范中寻找解释的人来说,静态块何时执行 - 就在这里。
JLS §8.7 说那 :
那么初始化是什么意思呢?让我们参考 JLS §12.4。 2.。这描述了详细的初始化过程。但是,JLS §12.4.1 在这里可能更合适。它说:
因此,要使静态初始化块自动执行,您必须强制执行这些选项之一。
First of all class loading is different than class initialization. For anyone looking for explanation from Java Language Specification, when is static block executed - here it is.
The JLS §8.7 says that :
So what does the initialization mean? Let's refer to JLS §12.4.2. This describes detailed initialization procedure. However point JLS §12.4.1 might be more appropriate here. It says that :
So to make the static initializer block to be executed automatically, you have to force one of those options to happen.
是的,你是对的,因为你没有使用
StaticClass
它不会被虚拟机加载,因此init()
永远不会被执行。对于第二个问题,您可能必须采取艰难的方式并扫描所有可用的类并加载它们。
https://stackoverflow.com/a/3223019/393657
Yes you are right, since you are not using your
StaticClass
it is not loaded by the vm and thereforeinit()
is never executed.For your second question, you probably have to go the hard way and scan all available classes and load them.
https://stackoverflow.com/a/3223019/393657
静态块在首先初始化或引用加载的类时执行。加载类并不意味着该类将被初始化。 JVM 类加载是需要关注的单独的事情。
Static block is executed when a loaded class is initialized or referenced first. Loading class doesnt mean that class is to initialized. JVM Class Loading is separate things to concern.
你是对的,最简单的方法是访问该类,例如在你的 main 方法中执行
StaticClass.class.newInstance();
或类似的操作。这将确保类由类加载器加载。
You are right, the easiest way is to access the class, for instance do a
StaticClass.class.newInstance();
Or something to that respect in your main method. This will ensure the class is loaded by the classloader.
当您的类(我猜是
StaticClass
)被引用时,静态代码就会被执行。因此,如果您创建
StaticClass
的新实例或者调用其静态方法之一,则应该执行它。The static code is executed when your class (
StaticClass
I guess) is referenced.Thus, it should be executed if you create a new instance of
StaticClass
or if you call one of its static methods.是的,静态初始化器将在类加载时执行。当您第一次访问类加载上下文中的类时,通常会发生这种情况。
Yes, the static initializer will be executed when the class is loaded. This normally occurs when you access the class in the class loading context for the first time.
在 b.jar 中,主方法类应该扩展该 StaticClass,然后自动调用静态块和 init()
in b.jar main method class should extend that StaticClass then automatically that static block and init() will be invoked
添加一些:
静态块将在jvm加载类时执行。
在您的示例中,您可以通过实例化类来调用
StaticClass
的init()
方法,例如
StaticClass staticClass=new StaticClass();
或
StaticClass.class.newInstance();
这更可取Adding some more:
static block will be executed when jvm load class.
Here in your example you can call
init()
method of yourStaticClass
by intantiating classlike
StaticClass staticClass=new StaticClass();
or
StaticClass.class.newInstance();
this is more preferebal