*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final member variable" Watch "final member variable" New topic
Author

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?
Thanks,
Jenny
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, §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


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final member variable
 
Similar Threads
final field in a method
member variables in servlet
Member Variables
Interface Ques
static vars in inner classes