Do final member variables have to be initialized explicitly? But non-final member variables do have to. Am I right? Thanks, Jenny
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
posted
0
Final member variables must be initialized explicitly in an initializer or constructor. Here's a snippet from the JLS, §8.3.1.2 final Fields:
A field can be declared final (�4.5.4). Both class and instance variables (static and non-static fields) may be declared final. It is a compile-time error if a blank final (�4.5.4) class variable is not definitely assigned (�16.7) by a static initializer (�8.7) of the class in which it is declared. A blank final instance variable must be definitely assigned (�16.8) at the end of every constructor (�8.8) of the class in which it is declared; otherwise a compile-time error occurs.
Non-final member variable do not need to be initialized before they can be used. If they are not initialized explicitly, they will be implicitly initialized to their default values. I hope that helps, Corey