[英]Memory consumption on Initializing final static variable with another final static
My question is, if I creates a final static variable in a class and initialize it with another final static variable (already declared and initialized). 我的问题是,如果我在一个类中创建一个最终的静态变量,并用另一个最终的静态变量(已经声明和初始化)对其进行初始化。 What would be the memory consumption for both the variables.
这两个变量的内存消耗是多少?
For example:- 例如:-
class SomeClass{
private static final byte VARIABLE_1 = 0x01;
private static final byte VARIABLE_2 = VARIABLE_1;
...
}
What would be the memory consumed by VARIABLE_1, VARIABLE_2 ? VARIABLE_1,VARIABLE_2会消耗多少内存?
Thanks in advance. 提前致谢。
In your example, and at runtime, 0 bytes. 在您的示例中,在运行时为0个字节。
final static
primitives are in-lined by the compiler they are constants. final static
原语由编译器内联,它们是常量。
class files contain a copy of the constant value of any static final fields it uses.so memory it is using is negligible. 类文件包含它使用的任何静态final字段的常量值的副本。因此它使用的内存可以忽略不计。
Class SomeClass
declares two constants, VARIABLE_1
and VARIABLE_2
, and initializes them with expressions that are compile-time constants. SomeClass
类声明两个常量VARIABLE_1
和VARIABLE_2
,并使用作为编译时常量的表达式对其进行初始化。
The compiler knows that VARIABLE_1
represents the value 0x01
and VARIABLE_2
represents the value 0x01
. 编译器知道
VARIABLE_1
表示值0x01
而VARIABLE_2
表示值0x01
。 When the SomeClass
class is loaded by a Java Virtual Machine, VARIABLE_1
and VARIABLE_2
are not stored as class variables in the method area. 当Java虚拟机加载
SomeClass
类时, VARIABLE_1
和VARIABLE_2
不会作为类变量存储在方法区域中。
The VARIABLE_1
and VARIABLE_2
fields are not class variables, they are constants, the Java compiler places the constant int values they represent into the constant pool of any class that uses them. VARIABLE_1
和VARIABLE_2
字段不是类变量,它们是常量,Java编译器将它们表示的常量int值放入使用它们的任何类的常量池中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.