Do final member variables have to be initialized explicitly? But non-final member variables do have to. Am I right? Thanks, Jenny
Joined: Dec 20, 2001
Final member variables must be initialized explicitly in an initializer or constructor. Here's a snippet from the JLS, §18.104.22.168 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