java,运行时或编译时何时加载静态变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4343760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:14:04  来源:igfitidea点击:

when is static variable loaded in java, runtime or compile time?

java

提问by suprasad

When is static variable loaded, runtime or compile time? Can someone please explain it.

什么时候加载静态变量,运行时还是编译时?有人可以解释一下吗。

I would really appreciate the help.

我真的很感激你的帮助。

Thank you.

谢谢你。

采纳答案by Ralph

They are loaded at runtime.

它们在运行时加载。

Static means: that the variable belong to the class, and not to instances of the class. So there is only one value of each static variable, and not n values if you have n instances of the class.

静态意味着:变量属于类,而不属于类的实例。因此,每个静态变量只有一个值,如果您有 n 个类的实例,则没有 n 个值。

回答by Jigar Joshi

run time when class is loaded. - Have a look at initialization

加载类时的运行时间。-看看初始化

回答by musiKk

How would you load a variable at compile time? The variable is initializedwhen the corresponding class is loaded. See the JVMS.

如何在编译时加载变量?该变量在加载相应的类时初始化。请参阅JVMS

回答by Adriaan Koster

The compiler optimizes inlineable static finalfields by embedding the value in the bytecode instead of computing the value at runtime.

编译器通过在字节码中嵌入值而不是在运行时计算值来优化可内联静态最终字段。

When you fire up a JVM and load a class for the first time (this is done by the classloader when the class is first referenced in any way) any static blocks or fields are 'loaded' into the JVM and become accessible.

当您启动 JVM 并第一次加载类时(这是由类加载器在第一次以任何方式引用类时完成的)时,任何静态块或字段都被“加载”到 JVM 中并变得可访问。

A demonstration:

演示:

public class StaticDemo {

 // a static initialization block, executed once when the class is loaded
 static {
  System.out.println("Class StaticDemo loading...");
 }

 // a constant
 static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000;

 // a static field
 static int instanceCounter;

 // a second static initialization block
 // static members are processed in the order they appear in the class
 static {
  // we can now acces the static fields initialized above
  System.out.println("ONE_DAY_IN_MILLIS=" + ONE_DAY_IN_MILLIS
    + " instanceCounter=" + instanceCounter);
 }

 // an instance initialization block
 // instance blocks are executed each time a class instance is created,
 // after the parent constructor, but before any own constructors (as remarked by Ahmed Hegazy)
 {
  StaticDemo.instanceCounter++;
  System.out.println("instanceCounter=" + instanceCounter);
 }

 public static void main(String[] args) {
  System.out.println("Starting StaticDemo");
  new StaticDemo();
  new StaticDemo();
  new StaticDemo();
 }

 static {
  System.out.println("Class StaticDemo loaded");
 }

}

Output:

输出:

Class StaticDemo loading...
ONE_DAY_IN_MILLIS=86400000 instanceCounter=0
Class StaticDemo loaded
Starting StaticDemo
instanceCounter=1
instanceCounter=2
instanceCounter=3

Notice how 'Starting StaticDemo' does not appear as the first line of output. This is because the class must be loaded beforethe main method can be executed, which means all static fields and blocks are processed in order.

请注意“Starting StaticDemo”如何不显示为第一行输出。这是因为必须执行 main 方法之前加载类,这意味着所有静态字段和块都按顺序处理。

回答by user207421

Loading is a runtime operation. Everything is loaded at runtime.

加载是一个运行时操作。一切都在运行时加载。

回答by Peter Lawrey

The static fields are loaded when the class is loaded. This usually happens which the file object of a class is created, but it can be earlier if the class is used another way.

加载类时加载静态字段。这通常发生在创建类的文件对象时,但如果以其他方式使用该类,则可能会更早。

The static initialiser is thread safe and you can access the class in multiple threads safely. This is useful as a way to create a thread safe singleton without having to use a lock.

静态初始化程序是线程安全的,您可以安全地在多个线程中访问该类。这是一种无需使用锁即可创建线程安全单例的方法。

Note: the class can be loaded (and its static intialisation block run) more than once if multiple class loaders are used. Generally, loading the same class in multiple class loaders can be confusing and is avoided, but it is supported and does work.

注意:如果使用多个类加载器,则可以多次加载类(及其静态初始化块运行)。通常,在多个类加载器中加载同一个类可能会造成混淆并被避免,但它受到支持并且确实有效。

回答by Ben Tennyson

When you type java ClassNamethen class loads into JVM with static variables, so you don't need an object for it.

当您键入时,java ClassName类将使用静态变量加载到 JVM 中,因此您不需要它的对象。

Where as instance variable loaded by JVM when the object is created.

在创建对象时作为 JVM 加载的实例变量。