File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Compile-time Constants Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Compile-time Constants" Watch "Compile-time Constants" New topic
Author

Compile-time Constants

Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
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
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Dec 19, 2002
Posts: 229
Thanks, Marlene.
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
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?


"If anything can go wrong, it will"
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
"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

Joined: Mar 05, 2003
Posts: 1391
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 ]
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
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

Joined: Mar 05, 2003
Posts: 1391
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
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

Joined: Mar 05, 2003
Posts: 1391
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.�
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile-time Constants