• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Compile time constant

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
what is a compile time constant?
is it a final variable?
I am not at all clear with it.

Can anyone help me in understanding the concept clearly
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile time constants are static finals whose values are known at compile-time.

eg. static final int i =100;


whereas load-time constants are the ones whose values are not known at compile-time but only at runtime.

eg. static final int i = getReturnFromSomeMethod();
 
Apna Apana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i have seen an example like this:


class JSC202 {
static byte m1() {final short s1 = 2; return s1;} // 1
static byte m2(final short s2) {return s2;} // 2
public static void main(String[] args) {
short s3 = 4;
System.out.print(""+m1()+m2(s3)); // 3
}}


where is error?
Ans: Compile time Error at line 2.

Please justify the answer.


Is it that any final variable is a compile time constant or a static final variable is only the compile time constant.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a "compile time constant" is just an abbreviation for what is called "compile time constant expression" in the language specification.
The definition is found here: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5313
Especially note "Simple names that refer to constant variables (�4.12.4)." and the link leads to this definition: "We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (�15.28) a constant variable."

So, static or not, is not of interest. But the type of the variable should be a primitive or a String. Thanks for the question, as I also haven't been sure about what "compile time constant" means yet. But now I am

Regarding the code: Have you tried to compile it? I don't get an error at line2, I get an error at line3: "Type mismatch: cannot convert from short to byte"

edit:
In short, you can say "compile time constants" are final variables of primitive type that are given a literal-value at the time of declaration. Literals itself are also called "compile time constant", so:

If I understood it right the variable i2 is a compile-time-constant, whereas i1 is not. That means i1 cannot be used as a case in a switch-statement, but i2 can.
[ May 17, 2007: Message edited by: Sasha Ruehmkorf ]
 
Space pants. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic