• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

final variables initialization

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i is blank final variable. You can initialize it in the constructor:
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use an initializer block as well...
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic