• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile time constants

 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me what is compile time constant.

if i declare a final variable like this

final int i=5;

This is a compile time constant

and if i declare like this
final int i;
i=5;

this is not a compile time constant

I want to understand what is a difference between above two intialization of final variables.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is dumb. It has a hard time connecting things across multiple lines. Consider that legally, there could be 800 lines of code between those two lines you posted in your second example.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The official definition of a "constant" is in the Java Language Specification (I think, here).

Do a search through this forum for "compile time constant" because that sort of question comes up regularly here. Some people say "runtime constant" but that does not appear to be an official Java term. You should find out about that from your search as well. Good luck with it
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic