Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

final static vs final non-static fields and JVM optimization

I'm curious how are the static final fields treated by the JVM. I saw a similar question here but it's not what I'm looking for. Let's consider such example:

public class TestClassX {
   public final int CODE_A = 132;
   public final int CODE_B = 948;
   public final int CODE_C = 288;
   // some other code
}

public class TestClassY {
   public static final int CODE_A = 132;
   public static final int CODE_B = 948;
   public static final int CODE_C = 288;
   // some other code
}

In TestClassX fields, as they are final and cannot be modified, have the same values in all instances of the TestClassX class. Of course I cannot write TestClassX.CODE_A but I can say, that these values are actually common for all instances - I'm sure, that each instance has a CODE_A field with the value 132.

In the TestClassY I can use the syntax TestClassY.CODE_A, but at a first sight it's only easier for a developer who sees "Oh, those values are common for all instances".

My main question: I guess that JVM, in case of TestClassX, doesn't use any extra memory for final fields each time a new instance is created. Does it? Does JVM make any optimization in this case and what kind of optimization it is?

Extra question 1) I'm also sure that I'm missing something very important here which is the cause of my doubts. What's that?

Extra question 2) Btw. How can I take a look at how my Java source code looks like after the JVM optimization (so I can use in in the future ;))? Does any IDE support such a functionality? IntelliJ for example? I would like simply to see how JVM treats my TestClassX and TestClassY.

like image 617
guitar_freak Avatar asked Sep 11 '15 08:09

guitar_freak


1 Answers

  • Non-static fields are always present in the instances. They do not save memory.
  • In general JVM does not optimize non-static fields. Even if they are final they can be still set to different value using reflection or during deserialization.
  • There is an experimental VM option -XX:+TrustFinalNonStaticFields (off by default) which tells JVM to optimize access to such fields, i.e. treat them as constants and eliminate field loads.
  • There is a -XX:+PrintAssembly VM option to dump JIT-compiled code.
like image 54
apangin Avatar answered Oct 12 '22 11:10

apangin