Poyan Gerami wrote:it should be 3 compile errors ?
"variable bench might not have been initialized"
they have count line 4 and 15 as compile errors, should be just line 4 ?
No! I agree with the book! 4 compiler errors it is. And more importantly, my
Java compiler agrees with me (and the book) as well
I don't think there is any discussion about lines 11 and 12: both initialize a final variable for the second time which will result in a compiler error (count = 2). Because
bench is not initialized on
line4 and it's not initialized in one of the static initializer blocks as well, you have a final class/static variable which is not initialized at all. That's a compiler error as well (count = 3). And finally on
line15 final class/static variable
bench is initialized in a method, which is again not allowed and will result in another compiler error (count = 4). You can only initialize a final class/static variable instantly (meaning on the same line as the declaration) or in a static initializer block; all other locations/attempts will result in a compiler error!
Here you'll find a nice overview of how to initialize class/static variables, instance variables and local variables. I would recommend reading it as it's very informative (with a code example as well).
Hope it helps!
Kind regards,
Roel