Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final variables initialization

 
Li Wenfeng
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
sam patel
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i is blank final variable. You can initialize it in the constructor:
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an initializer block as well...
 
Meg Adal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thinks rule about any (class level, method level, within an argument) final variable is that they can be assigned only once.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic