Sergej Smoljanov wrote:final variable always have no default value and must be internalized :
final static - if final static variable not initialed after static initialization block - compilation error
final instance - if final instance variable not initialized after constructor - compilation error
Almost correct!
We have 3 kinds/types of variables. Let's discuss each of them.
class variables
Class variables (also called static fields, static variables,...) get a default value if you don't initialize them. If you use/access (e.g. for printing) a class variable which is not initialized, your code will still compile.
If you mark a class variable final, you MUST initialize this variable. You'll have 2 ways to do so: assign a value instantly OR in a static initializer block. If you don't initialize a final class variable, your code will NOT compile.
instance variables
Instance variables (also called data members, member variables,...) get a default value if you don't initialize them. If you use/access (e.g. for printing) an instance variable which is not initialized, your code will still compile.
If you mark an instance variable final, you MUST initialize this variable. You'll have 3 ways to do so: assign a value instantly OR in a constructor OR in an initializer block. If you don't initialize a final instance variable, your code will NOT compile.
local variables
Local variables (also called method variables,...) do NOT get a default value. If you use/access (e.g. for printing) a local variable which is not initialized, your code will NOT compile. But if a local variable is declared and not initialized, your code will compile if you don't use/access (e.g. for printing) this local variable. That's a little tricky! These lines can be summarized into 1 simple rule: you MUST assign a value to a local variable before you use/access it; otherwise your code will NOT compile.
If you mark a local variable final the same rules apply as to the (regular) local variables (so that's easy
).
I have made a little code snippet where all the different possibilities are mentioned. So you can start experimenting to get a good understanding of variable initialization behavior. (note: CE stands for Compiler Error)
Hope it helps!