Do final static variables operations happen in run-time or compile time ? For example:
public static final int ID_1 = 1;
public static final int ID_2 = 2;
p
There is a hint here: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
It says:
If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value.
So once that is done and you end up with 1 + 2 in the method, it would be logical for that to be optimized as well and just use the 3 at compile time.
To prove it in practice, you can compile your code and then decompile it to see what's going on.
I tried with JD-GUI and this is what I got when decompiling your code:
public class TestCompileOrRuntime
{
public static final int ID_1 = 1;
public static final int ID_2 = 2;
public static int test()
{
return 3;
}
}
So it looks in this case the compiler is solving the operation in compile time.