Forums Register Login

Compile-time Constants

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks, Marlene.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
"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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
I'm wondering, where is it desirable to use a final parameter anyway?
[ June 02, 2003: Message edited by: Brian Joseph ]
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Arg, Marlene, I knew the answer to that! I just didn't think hard enough; Local classes, yes!
Is that the only case?
+Pie Number of slices to send: Send
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.�
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1038 times.
Similar Threads
Dan's Q - char to byte
Dan's Q doubt again?
Argument Scope
Help in Final variables
Dan's Mock Exam
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:21:16.