• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile time constants

 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since final variables are initialized before all so isn't the statement int a=b+4 actually int a=100+4 ???
 
Henry Wong
author
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankur kothari
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok understood....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic