I'm curious how static final fields are handled by the JVM. I saw a similar question here , but that is not what I am looking for. Consider this example:
public class TestClassX { public final int CODE_A = 132; public final int CODE_B = 948; public final int CODE_C = 288;
In TestClassX fields, since they are final and cannot be changed, have the same values ββin all instances of the TestClassX class. Of course, I can not write TestClassX.CODE_A , but I can say that these values ββare actually common for all instances - I am sure that each instance has a CODE_A field with a value of 132 .
In TestClassY I can use the syntax TestClassY.CODE_A , but at first glance it is only easier for the developer who sees "Oh, these values ββare common to all instances."
My main question: I think that the JVM in the case of TestClassX does not use extra memory for final fields every time a new instance is created. Does it have? Is the JVM any kind of optimization in this case, and what kind of optimization is it?
Additional question 1) I am also sure that I am missing something very important here, which is the reason for my doubts. What is it?
Additional question 2) PPC. How can I take a look at what the Java source code looks like after optimizing the JVM (so that I can use it in the future;))? Does any IDE support this functionality? For example, IntelliJ? I would just like to see how the JVM handles my TestClassX and TestClassY .
source share