Declaring a variable with the final keyword makes it impossible to reinitialize that variable once it has been initialized with an explicit value ( notice we said explicit rather than default
The above statement was not very clear to me since final variables are not assigned default values so what default values are being mentioned? Is it correct to say that is that If I initialize my final instance variable with a default value, like 0 for integers, I can assign a different value later at runtime? or If I only do the default value assignment and do not give it any other explicit value, will the compiler complain that my variable has not been initialized ?
What do you mean they're not assigned default values? Check out this code:
Obviously, the final variable someInt is assigned a default value. This initial assignment, however, does not prevent you from assigning a value to it one more time, as done in the constructor in my example. However, once that assignement has been done, performing another assignment is considered an error. I hope that helps, Corey
Corey, My issue is that what Im seeing in reality , ( compiling pieces of code like yours ) , doesnt match what Im reading, so Im trying to understand what Im missing. look at this:
Dont count on default value for final variables though, because a final variable -- even if it's an instance variable --- wont be given one. The rule is if you declare a final instance variable, you are obliged to give it a explicit value , and you must do so by the time the constructor completes.
So, I would expect your static initializer code not to compile .... See what I mean ?
Giselle Dazzi<br />SCJP 1.4
posted 17 years ago
Just played a little with your code Corey and it does compile and run like you said, but if I comment the line // someInt = 2; it does not compile anymore: finalTest.java [13:1] variable someInt might not have been initialized System.out.println(someInt); ^ 1 error So Im coming to the conclusion that final variables do get assigned default values but the compiler do not accept them so you have to make an explicit assignment in order for it to compile and after that, you can even use the default values assigned ?
Giselle, Let look at Corey's code with line numbers:
The trick to Corey's code is his use of a instance initializer on lines 4,5,6.
So Im coming to the conclusion that final variables do get assigned default values but the compiler do not accept them so you have to make an explicit assignment in order for it to compile and after that, you can even use the default values assigned?
To answer your question: I don't believe a "blank final variable" can ever use the default value, except in the context of the instance initializer. So if you use instance initializers--or static initializers; yes, such a beast exists too--you can use the default value, but in practice, instance initializers are fairly rare. They were introduced to support anonymous inner classes. Basically, when you comment out line 12 you're violating the caveat that the final variable must be explicitly assigned a value before any constructor completes. When the program enters the main method a call to the constructor is made, and the compiler wants to know that your going to feed it a final variable that has been "fattened for the kill." Otherwise the variable holds nothing but empty calories and the compiler is going to complain about not getting enough to eat! PS... You can also have static initializers. But in the context of the example...
A class can have any number of static or instance initializers. A static initializer is like a class, or static, method. It has no access to instance variables or methods. As the keyword static would suggest. [ May 12, 2003: Message edited by: Timothy Stone ]
Timothy Stone, MIT, SCJP
"This Satan's drink [coffee] is so delicious, we shall cheat Satan and baptize it." --Pope Clement the VIII (1592-1605)
posted 17 years ago
Giselle, If you look at my code, you can see that I really have to "trick" the compiler into displaying that default value. I'm using an instance initializer and a getter method in order to view that default value. It's highly doubtful that this is something you'd ever do in practice, but I knew a couple tricks, so I used them to get my point across. If you take out the instance initializer and tried to simply display the value of the final variable in the contstructor, prior to assigning a value to it, you'd get an error that would state that the variable may not have been initialized (just like trying to use a local variable before initializing it). So, in a very strict view of the language, final variables are given default values, but they're not easily accessible - you need to trick the compiler into giving it to you. Rather, the default value is treated as if it's not really there at all. I hope that helps, Corey