当我们第一次使用一个类时,静态代码总是被执行吗?
我们的应用程序使用的初始化代码取决于静态代码的执行顺序,我想知道这个顺序在所有 JVM 中是否一致。
下面是我的意思的一个示例:
public class Main {
static String staticVar = "init_value";
public static void main(String[] args) {
System.out.println(A.staticVar);
staticVar = "mainValue";
System.out.println(A.staticVar);
}
}
public class A {
static String staticVar = Main.staticVar;
}
will Give:
init_value init_value
和
public class Main {
static String staticVar = "init_value";
public static void main(String[] args) {
// System.out.println(A.staticVar);
staticVar = "mainValue";
System.out.println(A.staticVar);
}
}
public class A {
static String staticVar = Main.staticVar;
}
will Give (在我的环境中):
mainValue
总而言之,在所有 JVM 中,当我们第一次使用一个类时,静态代码是否总是执行?
Our application is using initialization code that depends on the order static code is executed and I'm wondering if this order will be consistent across all JVMs.
Here is a sample of what I mean:
public class Main {
static String staticVar = "init_value";
public static void main(String[] args) {
System.out.println(A.staticVar);
staticVar = "mainValue";
System.out.println(A.staticVar);
}
}
public class A {
static String staticVar = Main.staticVar;
}
will give:
init_value init_value
and
public class Main {
static String staticVar = "init_value";
public static void main(String[] args) {
// System.out.println(A.staticVar);
staticVar = "mainValue";
System.out.println(A.staticVar);
}
}
public class A {
static String staticVar = Main.staticVar;
}
will give (on my environment):
mainValue
To summarize, across all JVMs, is static code always executed when we use a class for the first time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
编辑:尽管有下面的所有保证,如果您正在考虑依赖这种事情,我会尽力重构您的代码,以便它不会突然出现。虽然它保证可以工作,但它也可能使您的代码变得非常脆弱。静态初始化器被“无形地”调用这一事实使得它们相对难以推理和调试。
是的,这是由语言规范保证的。来自规范的 第 8.7 节:
以及第 12.4 节:
EDIT: Despite all the reassurances below, if you're thinking of relying on this sort of thing, I would try hard to refactor your code so that it doesn't crop up. While it is guaranteed to work, it's also likely to make your code very brittle. The fact that static initializers get called "invisibly" makes them relatively hard to reason about and debug.
Yes, this is guaranteed by the language specification. From section 8.7 of the spec:
And from section 12.4:
当您第一次使用一个类时,静态初始化程序(例如您的 staticVar 声明)总是被执行。
静态方法仅在被调用时执行。在您的情况下,发生这种情况是因为 static void main(String[] args) 是您的应用程序的入口点。但是如果您定义了不同的静态方法,那么它就不会被调用。
还可以创建一个静态初始化块,该块也会在首次使用该类之前自动调用,例如
Static initialisers (e.g. your staticVar declarations) are always executed when you use a class for the first time.
Static methods are only executed when they are called. In your case, this is happening because the static void main(String[] args) is the entry point to your application. But if you defined a different static method then it would not be called.
It is also possible to create a static initialiser block that will also be called automatically before the class is first used, e.g.
所有平台上应该都是一样的。请参阅 JVM 规范:http://java. sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#19075
That should be all the same on all platforms. See JVM Specification : http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#19075
引用 Java 规范:
A quote from Java specification:
是的,根据 Java 语言规范静态代码始终在所有(兼容的)JVM 上以相同的顺序执行。
8.7 静态初始化器
Yes, according to the Java Language Specification static code is always executed in the same order on all (compliant) JVM's.
8.7 Static Initializers
与其他海报相同,但让我补充一点,在我看来,依赖于其他类的状态的静态初始化程序(如您的示例)似乎会导致非常脆弱且难以维护的代码。我将使用静态初始化器来填充内部类数据——用默认值或类似的东西填充数组。但我想我从来没有看过另一堂课的数据。从技术上讲是合法的,也许在某些奇怪的情况下这是个好主意,但是很糟糕。
Dittos to other posters, but let me add that a static initializer that depends on the state of other classes like in your example seems to me to make for very fragile and difficult-to-maintain code. I'll use static intializers to populate internal class data -- to fill up an array with default values or that sort of thing. But I don't think I've ever had one peek at the data in another class. Technically legal, and maybe there are odd cases where it's a good idea, but yuck.
是的,我相信这就是定义的重点。
Yes, I believe that would be the point by definition.
静态块总是首先运行,而不仅仅是第一次...在执行任何代码块之前,
JVM首先执行静态代码块
,然后才运行该代码块因为它已经被设计...Static Block will always run first not just the first time... Before executing any code block,
JVM execute the static code block first
, and then only it run the code block as it has been designed...