File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final member variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final member variable" Watch "final member variable" New topic

final member variable

Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Do final member variables have to be initialized explicitly? But non-final member variables do have to. Am I right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Final member variables must be initialized explicitly in an initializer or constructor.
Here's a snippet from the JLS, § 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,

SCJP Tipline, etc.
I agree. Here's the link:
subject: final member variable
jQuery in Action, 3rd edition