This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I tried building a method with a configurable switch statement, to test how method-local parameters and variables work when declared final. A switch statement needs constant expressions for the case statement, and the final local variables/parameters are constant. Do I get a compiler error because the case labels are potentially not constant at compile time? Here are relevant snippets of the code:
I even tried assigning a value to a local final variable through the parameters:
But I had the same error in both cases:
Is this because these final varables must be declared and initialized before compilation? I thought the answer was 'no' because you can initialize final variables through constructors with no problem. But my results tell me that I don't really understand when a final variable is a constant and when it's not. Note that assigning a local value to one of the final variables removes the compiler error: final int x = 2; Any help? Thanks! Regards, Jeff
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)