• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

final reference variables

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't declare an instance final reference variable without initializing (as expected) but I can do it inside a method:

(Say I'm in main)


Why is this? Is it because they want to prevent a final instance reference variable from getting a default 'null' value? Why is it legal in the method?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you can not use the local reference variable(within a method) without initializing it, usually the variable is safe to be a uninitialized final local variables. Ofcourse as you said, before using that variable you should either initialize or remove final modifier.
 
nico dotti
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so am I right that the fact that (if it were allowed to be declared it would get a default value) is the reason it is in fact NOT allowed? That is to say is the reason that one can't declare a final reference var in 'instance space' is because the language is trying to protect you from inadvertantly having a final reference variable that is stuck being null?
 
Thirugnanam Saravanan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can declare a final instance reference variable without an initializer. But you must make sure you initialize it within either an initializer block or all constructors. This is legal:

But note that this program isn't legal:

and neither is this:
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kelvin, please explain in detail why the following code is illegal:



is it because we are not initializing final instance variable in the overloaded constructor?

And in the following code is it just because we are initializing final instance variable in a method and not within a constructor?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ushma,

Your statements are correct. Final instance variables must be definitely assigned by the end of every constructor of their class, so you can't have constructors that don't assign to the variables. Also, only constructors and initializer blocks can assign values to final instance variables. The exact set of rules are a bit complex, based on Java's precise definition of "definite assignment"; see the JLS for more details if you're interested.

Some other subtleties to note: a constructor can call another constructor to do the assignment, like this:

And the assignment can also be done in an initializer block, like this:

However, you cannot assign more than once to a final instance variable. So these two programs are both illegal:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic