aspose file tools*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compile time constant?" Watch "compile time constant?" New topic
Author

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
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
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
Sheriff

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


JavaBeginnersFaq
"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;
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compile time constant?