aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final variables initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final variables initialization" Watch "final variables initialization" New topic
Author

final variables initialization

Li Wenfeng
Greenhorn

Joined: Aug 26, 2002
Posts: 22
When I compile the following code:
class A{
final int i;
}
It generates compiler error: "variable i might not have been initialized"
but,In Brian's SCJP book, it says:

The following code is legal, even though the final variable is not initialized within the same line on which it is declared:
public final int users; // Does not initialize a final variable

also,He said this several times in his book.
What's the truth? I think Brian made a mistake!
thanks!


SCJP2
sam patel
Ranch Hand

Joined: Jun 13, 2002
Posts: 103
Originally posted by Li Wenfeng:
When I compile the following code:
class A{
final int i;
}
It generates compiler error: "variable i might not have been initialized"
but,In Brian's SCJP book, it says:

also,He said this several times in his book.
What's the truth? I think Brian made a mistake!
thanks!


I think Brian made a mistake...
You need to initialize a final variable.....
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
i is blank final variable. You can initialize it in the constructor:
Jack Lau
Ranch Hand

Joined: Aug 30, 2002
Posts: 166
Originally posted by Anthony Villanueva:
i is blank final variable. You can initialize it in the constructor:

You can declare final member without initialization in one line but it is necessary to initialize it in the constructor.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
You can use an initializer block as well...
Meg Adal
Ranch Hand

Joined: Mar 12, 2002
Posts: 70
I am not sure but remember of reading somewhere that
"final variable must be initialized before constructor ends(ie controls goes out of constructor)"
So we can initialize it in a block or in contructor
Correct me if I am wrong
Megan
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Megan:
I think you are right. If final is a member data field, it has to be initialized at the declaration or in a subsequent initilizer or at most in the constructor. Otherwise, when initialized in some other place, compiler will complain that value can not be assigned to a final variable. Interestingly, if you forget to initialize a final and NEVER use it in your code, it would compile and run fine.
However, if final is method variable, you can initialize it before its first use....
Thanks
Barkat
Li Wenfeng
Greenhorn

Joined: Aug 26, 2002
Posts: 22
Another thing, you can't initialize a class-level final variable in both the initializer block and constructor.

It will complain about: "variable i might already have been assigned to" in line 7.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I thinks rule about any (class level, method level, within an argument) final variable is that they can be assigned only once.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final variables initialization