• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Compile-time Constants

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is related to one of the questions in Dan Chisholm's Single Topic Exams.
In the following code:

He refers to char c in method m1() as a compile time constant. However, the final char c in the argument list of method m3() is not a compile-time constant.
I thought that a variable's being declared final would make it a compile-time constant. Any other factors invovled here?
Thanks,
Saket
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple name that refers to a final variable whose initializer is a constant expression.
The formal parameter declaration final char c of the method m3 does not have an initializer.
JLS 15.28 Constant Expression (last section) defines compile-time constant expressions.
 
Saket Barve
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Marlene.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to make sure that the point of this code.
Since third and forth methods do not have final values at compile time, their return value should be casted to fit in the return type. Is this correct?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Is this correct?" almost
"Since third and forth methods do not have final values at compile time," - well... I think you understand, but let's work on the explanation...
The variables c and s are declared final, but they do not have initializers that are constants. Therefore, they are not compile-time constants.
"their return value should be casted to fit in the return type." - Yes.
final char c; //c is not a compile-time constant
final char d = 10; //d is a compile-time constant
final char e = c + 1; //e is not a compile-time constant
final char f = d + 2; //f is a compile-time constant
void m(final char g) {} //g is not a compile-time constant
[ May 31, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte m(final char c) { return c; } //compiler-error
c is declared final. The reason c is not a compile-time constant is that the declaration of c does not have an initializer that is a constant expression.
What a crummy explanation. Here is a more intuitive explanation.
The reason c is not a compile-time constant is that the compiler cannot know at compile-time what the value of c is.
[ May 31, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm wondering, where is it desirable to use a final parameter anyway?
[ June 02, 2003: Message edited by: Brian Joseph ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Brian Joseph
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arg, Marlene, I knew the answer to that! I just didn't think hard enough; Local classes, yes!
Is that the only case?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good, I am glad you understand final parameters referenced by inner classes.
Is that the only case? The Java Programming Language (where I get many insights into the language) says �Local variables and parameters are usually declared final only when they will be accessed by a local, or anonymous, inner class�though some people advocate always making parameters final, as a matter of style.�
 
reply
    Bookmark Topic Watch Topic
  • New Topic