• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

assigning a value to a final

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to declare a final, or a final static and then assign the value to it later in the program?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarence
Sure you can. something like this is perfectly legal:

The key to remember is that you have to ensure that final variables are assigned a value in every possible execution path. But, only once per execution path.
For more details check out the JLS chapter 16
 
Clarence Kirby
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what if I declare a global final for some class and when the constructor (the only constructor) gets called I initialize the final then?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by global?
 
Clarence Kirby
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is what I mean:
public class blah {
final static int whatever ;
...
public blah(){
whatever = 5 ;
}
...
}
is that valid
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It must be assigned a value because it's static and final. You can't assign it in the constructor, because it may never get executed if no instance of the class is created.
 
Clarence Kirby
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if no instance is created that variable never exists. The class must be instantiated since its not abstract. Right?
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarence
It is a static variable so it can referenced by the the class name without having an instance of the class created prior.
In another class it could be referenced as:
blah.whatever
You can create a static initializer that will initialize the variable so that when the class is loaded it gets a value.
Something like this:

Doing that lets you do any kind of complex processing you might need to do to determine what value to assign.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The articles Java Code Talk, Part 3 and Part 4 by Josh Bloch and Neal Gafter atinformIT discusses this subject. You'll have to register with informIT to view the articles - registration is free.
 
Clarence Kirby
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so what if it's not a static variable? Would I still not be able to initialize it in the constructor?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Clarence Kirby:
ok, so what if it's not a static variable? Would I still not be able to initialize it in the constructor?


Have you tried? The compiler can probably give you even more reliable information than this forum (and faster, too)...
As far as I'm concerned, I think you would be able to do so (that is, initialize a final, non-static member variable in the constructor).
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Clarence Kirby:
ok, so what if it's not a static variable? Would I still not be able to initialize it in the constructor?


The key here is to make sure you initialize it in
every constructor. This is a variation of making sure a final variable is initialized ion all possible execution paths.
 
Clarence Kirby
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a bunch all!
 
The City calls upon her steadfast protectors. Now for a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic