• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

compile time constant?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic