the above code doesnt compile.The explaination given is: case expression must be a constant expression. Final objects are not allowed in a case statement. A final object?s value can be changed whereas a final variable's value cannot be changed. My question is how we can change the value of objects of wrapper classes which are final?
In case of final variables to primitive types, you can not change its value, because, in memory, the value of that variable is in the same memory slot that the variable itself, but in case of final variables that refer to objects, the only thing is final is the reference to the object, I mean, in an variable that refer to an object the only thing that is on that variable is the memory address to the object, so, you can not make:
The object itself is on other memory place (called the "heap") and it's not final so, lets say your class A has a public int attribute called aa that is not final, like:
So it is allowed to change the aa's value, like:
As I know, you can not change the value inside a wrapper class, but the compiler does not see the wrapper class, it just see it is an object and that is not allowed in a switch-case statement.
Hope this is clear, regards, [ December 29, 2008: Message edited by: Alejandro Galvan ]
Thanks Alejandro for the quick response but i was confused with the final objects of wrapper classes.
Like here we have final Integer a = 1; now according to me we cannot change the value of the object a (neither can we change the refrence as it is marked final) so here why it is not taken as a compile time constant and use it in case statement. My question is can final objects of wrapper classes change their value ??
The answer is no (a final wrapper reference will always have the same value.) There is no reason why it couldn't be used as a compile time constant if the compiler can determine its value at compile time. It's just that the specification at this point doesn't consider them to be "compile time constants" (the specification defines what a compile time constant is, and it is free to make any decisions to that regard that may seem arbitrary.) Also, the specification (like Henry pointed in the other post) might change.
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben is right here, java considers only final primitives and Strings as compile time constant.
And as you know case statements allow only primitives upto int(byte, char ,short), so you cannot use any reference type in case statement. If you make an Integer final, than you are writing as reference type in case, so its not allowed.
Another case where compile time constant needed is in inner class where only static fields allowed that are compile time constant.
final static int constInt=5; final static String constStr="abc"; final static byte b=5; final static long l=5; final static float f=5; final static double d=5; final static Integer i=5;//error static int i=5;//error final static String s;//error s="";//error
Thanks for the explanation and the fact regarding the inner class. "Another case where compile time constant needed is in inner class where only static fields allowed that are compile time constant."
The above is not mentioned in the K&B book (or I overlooked it ?) . Could you please let me know the source of it.
Thanks in advance,
It will give me the powers of the gods. Not bad for a tiny ad: