Answer the question
In order to leave comments, you need to log in
What happens when you access static constants?
For example:
public class Solution {
static final int С = 55 + 10;
static final int A = 55 + 10;
}
class Solution2 {
public static void main(String[] args) {
System.out.println(Solution.C);
}
}
Solution
been loaded into the pool of constants, or only C
? Solution.C
will it be checked if it is in the constant pool, and if so, is the already initialized value taken from there? Answer the question
In order to leave comments, you need to log in
Firstly, the compiler will fold the constants, so there will be no addition in the class file, C
and A
they will be equal to 65. Secondly, when accessing any static field, the class will be fully loaded into metaspace and the constant pool will be fully initialized.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question