Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assigning a value to a final

 
Clarence Kirby
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?
 
Dave Vick
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?
 
Dirk Schreckmann
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
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 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?
 
Ilja Preuss
author
Sheriff
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic