Search code examples
javamemorykeywordfinalallocation

Optimizations for Final Keyword


In Java, the final keyword seems to separate construction from initialization, which suggests an intent to block memory usage for objects in a similar manner to Arrays.

if I have a non recursive Object Hierarchy, and all the fields are final, that memory only needs to be allocated once, and might as well be allocated contiguously for the cache. Does the JVM actually do this? and if no, why not?


Solution

  • The short answer is:

    There are no class/method optimisations in Hotspot based on whether or not a class or method is final.

    You can read more in this article.