简体   繁体   中英

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 ?

Thanks in advance.

In your example, and at runtime, 0 bytes. final static primitives are in-lined by the compiler they are constants.

class files contain a copy of the constant value of any static final fields it uses.so memory it is using is negligible.

Class SomeClass declares two constants, VARIABLE_1 and VARIABLE_2 , and initializes them with expressions that are compile-time constants.

The compiler knows that VARIABLE_1 represents the value 0x01 and VARIABLE_2 represents the value 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.

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.

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