补充: 我的说法可能是错误的。 首先确定,java 没有静态数据区。还是按照jvm规范里的说法: 3.5 Runtime Data Areas 几个部分: The pc Register,Java Virtual Machine Stacks,Heap,Method Area,Runtime Constant Pool,Native Method Stacks 其次,static fields存在什么地方。 jvm规范,里面只是在 2.17.3 Linking: Verification, Preparation, and Resolution 中讲到,Preparation involves creating the static fields for a class or interface and initializing such fields to the standard default values。 问题是这里并没有介绍到底是在哪里创建了static fields,这里并没有介绍。 然后再看《Inside the Java Virtual Machine>>这本书里的说法: http://www.artima.com/insidejvm/ed2/jvm5.html In addition to the basic type information listed previously, the virtual machine must also store for each loaded type: * The constant pool for the type * Field information * Method information * All class (static) variables declared in the type, except constants * A reference to class ClassLoader * A reference to class Class Class VariablesClass variables are shared among all instances of a class and can be accessed even in the absence of any instance. These variables are associated with the class--not with instances of the class--so they are logically part of the class data in the method area. Before a Java virtual machine uses a class, it must allocate memory from the method area for each non-final class variable declared in the class. Constants (class variables declared final) are not treated in the same way as non-final class variables. Every type that uses a final class variable gets a copy of the constant value in its own constant pool. As part of the constant pool, final class variables are stored in the method area--just like non-final class variables. But whereas non-final class variables are stored as part of the data for the type that declares them, final class variables are stored as part of the data for any type that uses them. 也就是说,这里认为static的数据是放在方法区里的. 这里http://dave-robinson.net/tuning/jvm_details.html也有概括,当然说法同样来自上面这本书.另外,static变量保存在堆里的说法见: http://topic.csdn.net/u/20080924/06/d4e0c8cf-72e4-40db-a839-c2794290ccf8.html中7楼axman的说法: .... private static int A=3; 在哪里? 在堆里. 在堆里的A.class这个对象创建时的字段.对于Class类来说,它的实例A.class中有一个非静态字段int A. 因为它是Class类的一个实例的字段,所以被该Class类的实例A.class所表示的类的实例共同引用,对于A.class所表示的类的 实例来说它们是静态的. .... 我认为,从实现的角度来考虑,这是可行的,axman对java的虚拟机机制比较熟悉,做出这个判断我不知道他是推测还是能够证明.这个我就不知道如何去具体的分析了.
另一说:JAVA的static数据存在栈里。
记住就行了,要证明的话自己去看虚拟机规范,去分析虚拟机的代码,别人说的未必是正确的。
这个问题实在是无聊,Java 虚拟机规范那本书里面有介绍,运行时数据区一共几部分我也贴过了,不想再说。这个对你学习java基本没有什么意义
我的说法可能是错误的。
首先确定,java 没有静态数据区。还是按照jvm规范里的说法:
3.5 Runtime Data Areas
几个部分:
The pc Register,Java Virtual Machine Stacks,Heap,Method Area,Runtime Constant Pool,Native Method Stacks
其次,static fields存在什么地方。
jvm规范,里面只是在
2.17.3 Linking: Verification, Preparation, and Resolution
中讲到,Preparation involves creating the static fields for a class or interface and initializing such fields to the standard default values。
问题是这里并没有介绍到底是在哪里创建了static fields,这里并没有介绍。
然后再看《Inside the Java Virtual Machine>>这本书里的说法:
http://www.artima.com/insidejvm/ed2/jvm5.html
In addition to the basic type information listed previously, the virtual machine must also store for each loaded type: * The constant pool for the type
* Field information
* Method information
* All class (static) variables declared in the type, except constants
* A reference to class ClassLoader
* A reference to class Class Class VariablesClass variables are shared among all instances of a class and can be accessed even in the absence of any instance. These variables are associated with the class--not with instances of the class--so they are logically part of the class data in the method area. Before a Java virtual machine uses a class, it must allocate memory from the method area for each non-final class variable declared in the class.
Constants (class variables declared final) are not treated in the same way as non-final class variables. Every type that uses a final class variable gets a copy of the constant value in its own constant pool. As part of the constant pool, final class variables are stored in the method area--just like non-final class variables. But whereas non-final class variables are stored as part of the data for the type that declares them, final class variables are stored as part of the data for any type that uses them.
也就是说,这里认为static的数据是放在方法区里的.
这里http://dave-robinson.net/tuning/jvm_details.html也有概括,当然说法同样来自上面这本书.另外,static变量保存在堆里的说法见:
http://topic.csdn.net/u/20080924/06/d4e0c8cf-72e4-40db-a839-c2794290ccf8.html中7楼axman的说法:
....
private static int A=3; 在哪里?
在堆里.
在堆里的A.class这个对象创建时的字段.对于Class类来说,它的实例A.class中有一个非静态字段int A.
因为它是Class类的一个实例的字段,所以被该Class类的实例A.class所表示的类的实例共同引用,对于A.class所表示的类的
实例来说它们是静态的.
....
我认为,从实现的角度来考虑,这是可行的,axman对java的虚拟机机制比较熟悉,做出这个判断我不知道他是推测还是能够证明.这个我就不知道如何去具体的分析了.
比较赞赏你的看法,可你的看法还是模糊。
C++有专门的静态存储区,JAVA 没有PS ,大一曾经问过老师,3个教授回答的答案完全不同了,然后他们给我的一致答复就是:细节的别去研究了
这些细节的确没有必要研究,有精力还是多弄点实际的项目和理解一些设计模式.
但是,关于JAVA没有静态存储区一说
《think in java》明确说
程序运行时,数据可以保存到以下六个地方:
寄存器 堆栈 堆 静态存储区 常数存储区 非RAM存储区