Re: public static final - compilation error
- From: Thomas Fritsch <i.dont.like.spam@xxxxxxxxxxx>
- Date: Tue, 28 Jun 2005 17:59:12 +0200
Kamil Roman wrote:
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:
variable PLUS is declared final; cannot be assigned.
The code is:
public class Stale { public static final String[] OPERATORS = new String[] {
new String("+"),
new String("-"),
new String("*"),
new String("/"),
};
public static int PLUS; //final
public static int MULTI; //final
public static int MINUS; //final
public static int DIVIDE;//final
public Stale()
{
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;
}
}
}
-- 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.
--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@').
- Follow-Ups:
- Re: public static final - compilation error
- From: Chris Smith
- Re: public static final - compilation error
- References:
- public static final - compilation error
- From: Kamil Roman
- public static final - compilation error
- Prev by Date: Re: public static final - compilation error
- Next by Date: Re: public static final - compilation error
- Previous by thread: Re: public static final - compilation error
- Next by thread: Re: public static final - compilation error
- Index(es):
Relevant Pages
|