In the second case the i is declared inside the initializer block. So it is accessible only inside the initializer. So it is not compulsory to initialize it.
But you must initialize a final field which is accessible at the class level. That is why the first code is giving an error...
The fact is that final variables (whether instance or static) do not get default values from the compiler. For a final static variable, you have to initialize it in its declaration, or in a static initialization block. For a final instance variable, you have to initialize it in its declaration, in an instance initialization block, or in the class constructor (in every constructor of the class, as a matter of fact.) [ January 01, 2009: Message edited by: Ruben Soto ]
All code in my posts, unless a source is explicitly mentioned, is my own.
Jolly Tiwari
Ranch Hand
Joined: Mar 26, 2006
Posts: 77
posted
0
Ruben,
I would like to share my experience related to static initialization of final variables.
public static void main(String...arg) { System.out.println("in main=="+var);
}
}
Output is : 0 22
[ January 01, 2009: Message edited by: Jolly tiw ]
Jolly,
That's very interesting. I don't quite know what is going on there. It would appear that the final variable is getting a default value of 0, but that defies logic. If you remove the initialization to 22, the code won't compile. Would you mind expanding on your understanding of what is going on?
Also, the fact that you can call a constructor from a static initialization block of the same class is very strange. I really do not understand what is going on there. [ January 02, 2009: Message edited by: Ruben Soto ]
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
I think it is some type of trick, as see this code:
Jolly Tiwari
Ranch Hand
Joined: Mar 26, 2006
Posts: 77
posted
0
@punit and rouben
It seems as compiler ensures this thing only that we could not refer a final static variable before initializing it but if there are any references to that variable inside a static function or it is referred inside constructor and call to any of them is before that initialization , In both the cases such variables will take their default values.
Thanks for clarifying that. Viewed as a special case or "trick" it makes sense. It doesn't mean the final variable is assigned a default value however. I instead view it as the compiler replacing any use of that variable in the context you mentioned by a default value, but not assigning that default value to the variable, since that is impossible (if that were the case, then all final variables would only be able to hold their default values, since you can only assign a value to a final variable once.)
Ankit,
I see that what you say is true, but I think the mechanism is a little different. Non-final variables do actually get assigned default values. I think what is going on with final variables is a little different, and it's what was leading to a lot of confusion.
Thanks, guys.
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
Ruben, Ankit is talking about non-static final, not non-final variable.
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
Punit Singh wrote:Ruben, Ankit is talking about non-static final, not non-final variable.
Indeed, Punit. Sorry for misreading your post, Ankit!
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.