Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final member variable

 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic