> Hi,
>
> is it possible to initialize a public static final member of a class in
> the class' constructor? I try to do this and I receive error message:
>
> variable PLUS is declared final; cannot be assigned.
{... code ...]
No, the static finals need to be initialized before that. Why don't you
use a static initializer block (static { /* ... */ }), which would be
called at class load, in which you can initialize your members.

Signature
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"
> Hi,
Hi!
> is it possible to initialize a public static final member of a class in
> the class' constructor? I try to do this and I receive error message:
[quoted text clipped - 31 lines]
> Thanks for help
> KR
No, you can't initialize 'static final' things in a constructor or any
non-static method. But you can (and probably want to) initialize them in
the class initializer:
static
{
for (int i = 0; i < OPERATORY.length; i ++)
{
if(OPERATORY[i].equals("+")) PLUS = i;
if(OPERATORY[i].equals("-")) MINUS = i;
if(OPERATORY[i].equals("*")) MULTI = i;
if(OPERATORY[i].equals("/")) DIVIDE = i;
}
}
Hence, there is no need for your constructor anymore.

Signature
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
Chris Smith - 28 Jun 2005 17:17 GMT
> No, you can't initialize 'static final' things in a constructor or any
> non-static method. But you can (and probably want to) initialize them in
[quoted text clipped - 12 lines]
>
> Hence, there is no need for your constructor anymore.
Unfortunately, this still does not work. The variables PLUS, MINUS,
MULTI, and DIVIDE are blank finals, and there are two rules that apply
here:
1. They must be guaranteed to be initialized by the time static
initialization is complete.
2. They must not be multiply assigned.
Both rules are applied according to the compiler's conservative
data/control flow analysis, as specified in the JLS. The actual
contents of the array are not considered in performing that analysis.
So the loop through the OPERATORY array is not a valid way of
accomplishing the initialization.
It's trivial to modify this code to use temporary variables, in order to
work around both restrictions on the direct use of finals. Here is the
result:
public class Stale
{
public static final String[] OPERATORS = new String[] {
"+", "-", "*", "/"
};
public final static int PLUS;
public final static int MULTI;
public final static int MINUS;
public final static int DIVIDE;
static
{
int plus = -1, minus = -1, multi = -1, divide = -1;
for (int i = 0; i < OPERATORS.length; i++)
{
if (OPERATORS[i].equals("+")) plus = i;
if (OPERATORS[i].equals("-")) minus = i;
if (OPERATORS[i].equals("*")) multi = i;
if (OPERATORS[i].equals("/")) divide = i;
}
assert plus != -1;
assert minus != -1;
assert multi != -1;
assert divide != -1;
PLUS = plus;
MINUS = plus;
MULTI = plus;
DIVIDE = plus;
}
}
(Incidentally, I also removed the pointless use of "new String", and
fixed the apparent typo with the array name.)

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Chris Smith - 28 Jun 2005 17:49 GMT
> PLUS = plus;
> MINUS = plus;
> MULTI = plus;
> DIVIDE = plus;
Oops. Obviously, that should be:
PLUS = plus;
MINUS = minus;
MULTI = multi;
DIVIDE = divide;

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Roedy Green - 29 Aug 2005 08:11 GMT
>No, you can't initialize 'static final' things in a constructor or any
>non-static method. But you can (and probably want to) initialize them in
>the class initializer:
The reason is a constructor will be called many times, at least
potentially, but a static initialiser block is guaranteed to be called
exactly once.
Java does not want to deal with the possibility of you changing your
mind about a final value.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
> Hi,
>
> is it possible to initialize a public static final member of a class in
> the class' constructor? I try to do this and I receive error message:
>
> variable PLUS is declared final; cannot be assigned.
<snip>
I would suggest using an enum with a string embedded in each instance if you
are running Java 1.5
--Paul