静态方法内存分配 [英] Static Method Memory Allocation

查看:110
本文介绍了静态方法内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有两个分类堆和堆栈。创建对象时,对象的内存存储在堆中。如果类具有静态方法,可以使用类名调用该怎么办。如果没有创建对象,那么它将如何分配内存?如果它在哪里分配内存?

We have two classifications heap and stack . When a object is created, memory for object is stored in heap. What if the class has static methods ,which can be called using class name. If object is not created then how will it allocate memory and if it does where will it allocate memory?

推荐答案

这取决于JVM,但静态字段通常存储在堆上的特殊对象中。 (您可以在堆转储中看到它)卸载ClassLoader时,它的类及其静态对象/字段也会被清理。

It depends on the JVM, but static fields are usually stored in a special object on the heap. (You can see it in a heap dump) When the ClassLoader is unloaded, its classes and their static "objects"/fields are also cleaned up.

唯一不同的东西关于静态对象是你无法获得它的引用。 (但您可以使用反射来访问字段)

The only thing different about the static "object" is you can't get a reference to it. (But you can use reflection to access the fields)

这篇关于静态方法内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆