• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compile time constant?

 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constant is a variable that is declared as final , therefore cannot be changed.I have read in some posts about the term "compile time constant", could someone explain what is meant by this term please?
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that since the value is known to be constant (and thus unchangeable) at compile time, the compiler is free to insert its value wherever it is referenced. This means that, whenever you change the value, you need to forcibly recompile all classes that reference it, because the compiler will not pick up on that.
A runtime constant, on the other hand, has its value set during (or most likely at the beginning) of a program run, and it just so happens that it is never changed (although, not being final, it could).
[ August 30, 2005: Message edited by: Ulf Dittmer ]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be final and the value could be set in the constructor. However, that would not be a compile-time constant.
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so ;
class comp
{
public final int const = 5;
comp()
{
....
}
const is recognised as a compile time constant because it is declared and initialised as a member variable : or

class comp
{
public final int const;
comp()
{
const = 5;
}

not recognised as a compile time constant as this has not been initialised at the same point as declaration

Correct or incorrect?
[ August 30, 2005: Message edited by: Daniel .J.Hyslop ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic