wood burning stoves*
The moose likes Beginning Java and the fly likes compile time constant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile time constant?" Watch "compile time constant?" New topic

compile time constant?

Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
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?

an island in the sun <br />with a language of many tongue?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41017
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 ]

Ping & DNS - my free Android networking tools app
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9044
It could be final and the value could be set in the constructor. However, that would not be a compile-time constant.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Daniel .J.Hyslop
Ranch Hand

Joined: May 23, 2005
Posts: 55
so ;
class comp
public final int const = 5;
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;
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 ]
jQuery in Action, 2nd edition
subject: compile time constant?
Similar Threads
Interface initialization
switch statement
regarding final
switch statement and compile time constatns.
Run-time constants and compile-time constant