This assignment is illegal for a final static field.
Not from the compiler's point of view. If the first assignment
should fail, you have introduced a SECOND assignment violating
the rule
4.5.4 final Variables
"A final variable may only be assigned to once."
You could change the code slightly, using a temporary
private static final Font font;
static {
Font t_font = new Font( "Arial", Font.PLAIN, 12 );
if ( t_font == null )
{
t_font = new Font( "SansSerif", Font.PLAIN, 12 );
}
font = t_font; // font is only assigned a single time
Keyword.font = font;
Still illegal.
The actual problem is caused by "Keyword.font" (I assume
that his containing class is named "Keyword"). Since the
static font field is final, the assignment to Keyword.font
is illegal. It should be an unqualified assignment, which
would require using a different local variable name (like
you suggested "t_font").
public class Keyword
{
private static final Font font;
static
{
Font t_font = new Font( "Arial", Font.PLAIN, 12 );
if ( t_font == null )
{
t_font = new Font( "SansSerif", Font.PLAIN, 12 );
}
// an unqualified assignment is required
font = t_font; // font is only assigned a single time
}
}