aspose file tools*
The moose likes Beginning Java and the fly likes assigning a value to a final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "assigning a value to a final" Watch "assigning a value to a final" New topic
Author

assigning a value to a final

Clarence Kirby
Greenhorn

Joined: Jun 18, 2002
Posts: 16
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

Joined: May 10, 2001
Posts: 3244
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


Dave
Clarence Kirby
Greenhorn

Joined: Jun 18, 2002
Posts: 16
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

Joined: Dec 10, 2001
Posts: 7023
What do you mean by global?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Clarence Kirby
Greenhorn

Joined: Jun 18, 2002
Posts: 16
Well, this is what I mean:
public class blah {
final static int whatever ;
...
public blah(){
whatever = 5 ;
}
...
}
is that valid
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Clarence Kirby
Greenhorn

Joined: Jun 18, 2002
Posts: 16
but if no instance is created that variable never exists. The class must be instantiated since its not abstract. Right?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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

Joined: Dec 10, 2001
Posts: 7023
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

Joined: Jun 18, 2002
Posts: 16
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

Joined: Jul 11, 2001
Posts: 14112
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).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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

Joined: Jun 18, 2002
Posts: 16
thanks a bunch all!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assigning a value to a final