[Logo]
Forums Register Login
compile time constants
Henry had once written in a post long time back that

Instance variables (that are not compile time constants) get initialized in the order that they appear in the source (from top to bottom, and left to right). And any forward reference to them, will have them at their default values.


what about compile time constants?
in what order do they get initialise?

Well, compile time constants are ... constants. The Java compiler should optimize out the variables entirely, and use the constant instead.

Or another way to look at it. Constant variables are initialized first -- before everything else.

Henry
Thanks Henry
class Forward
{
private int x=y+10;
private int y=5;

private int a=b+4;
private final int b=100;


}

why is there an error in the last one....isnt b initialised before everything else
 
Ankur kothari wrote:
why is there an error in the last one....isnt b initialised before everything else


Well, what is it about the error message that you don't understand?

Henry
since final variables are initialized before all so isn't the statement int a=b+4 actually int a=100+4 ???
 
Ankur kothari wrote:since final variables are initialized before all so ....


Note: final variables and compile time constants are not the same thing. The statement that final variables are initialized first, is untrue.


Ankur kothari wrote: ... isn't the statement int a=b+4 actually int a=100+4 ???


Probably. After the optimizer gets to it. However, there is nothing in the JLS that allows compile time constants to be exempted from the forward referencing rules... so, this is still illegal according to the spec.

Henry
ok understood....
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Oct 17, 2017 02:21:49.