简体   繁体   中英

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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