简体   繁体   English

何时为java中的静态变量分配内存?

[英]When is the memory allocated for a static variable in java?

All the objects of a class share the static variable. 类的所有对象共享静态变量。 But when is the memory allocated for the static variable? 但是什么时候为静态变量分配内存? Is it when the first object is created for the class? 是为类创建第一个对象的时候? Or does it happen even before any instance for the class is created? 或者甚至在创建类的任何实例之前发生了什么? Also instance variable is allocated memory at runtime. 实例变量也是在运行时分配的内存。 The memory for static variable is allocated at runtime or compile time? 静态变量的内存是在运行时还是编译时分配的?

When the class is loaded, at runtime. 在运行时加载类。 You can find the details here . 你可以在这里找到详细信息。

当类加载器加载类时,将分配所有静态变量的内存,并且只执行一次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM