0

I write java code like this:

final int value=0;

When I use "Svace Static Analyzer" to anylyze the code,it shows: This class contains an instance final field that is initalized to a compile-time static value, Consider making the field static---->

static final int value=0;

I know static value is loaded in compile time. Can anyone explain the advantage of compile load?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
user3239558
  • 1,757
  • 4
  • 18
  • 31
  • 2
    At this point the variable basically will only exist once and not every instance of the class will have a different integer with the value 0 - I would assume that this reduces the memory footprint of the application, especially if the class has many instances. – luk2302 Feb 18 '21 at 19:13
  • 3
    Not only that but the compiler can also inline the constant for optimization. – dan1st Feb 18 '21 at 19:15
  • [Inline very effectively](https://shipilev.net/jvm/anatomy-quarks/15-just-in-time-constants/) indeed. Oops, this is about JIT constants, not compile time constants. Still quite interesting (and efficiently inlined). – Kayaman Feb 18 '21 at 19:18
  • Does this answer your question? https://stackoverflow.com/questions/4343760/when-is-static-variable-loaded-in-java-runtime-or-compile-time – Ankur29 Feb 18 '21 at 19:18
  • Is "compile load" your personal euphemism for "constant value inlining"? I've never hear the term "compile load" before, and I can't seem to find any articles on the web using that term. It's difficult to answer your question when you use made-up terminology. – Andreas Feb 18 '21 at 21:09
  • Sorry for misunderstanding, I changed the title to "What is the advantage of static value loaded in compile time?" Hope it can make it easy to understand – user3239558 Feb 18 '21 at 22:13

1 Answers1

0

Scenario 1:

class Mango{
 final int marker = 10;
}
  1. In a class, when you declare a variable as final and initialize it during declaration, that means you do not want to modify it from anywhere.

  2. In that case, all of the created objects of the class maintained a similar value for this variable.

  3. On the other hand, whenever we declare a variable as static, then at the class level a single variable is created which is shared with the objects. Any change in that static variable reflects to the other objects operations.

  4. But actually, at points 1 and 2 we want to achieve point 3's behavior implicitly.

So, java force or suggest you create only one copy of the marker variable by declaring is static also.

class Mango{
 final static int marker = 10;
}

Which is memory efficient, cleaner, and non-ambiguous.

Scenario 2:

public class Mango {
    final int marker;
    
    Mango(int m){
        marker = m;
    }
}

This type of declaration is totally different. In that case, every instance of the Mango class has its own marker variable, and it can be initialized only one time.

The first type of declaration ensures class-level final behavior and the second type of declaration ensures the object-level final behavior.

Md Kawser Habib
  • 1,966
  • 2
  • 10
  • 25