Language Lawyer Question on final

R

Roedy Green

private static final Font font;
static {
Font font = new Font( "Arial", Font.PLAIN, 12 );
if ( font == null )
{
font = new Font( "SansSerif", Font.PLAIN, 12 );
}
Keyword.font = font;
}

Why does Javac complain that font is declared final?
It is only assigned a value once.
 
J

Jim

private static final Font font;
static {
Font font = new Font( "Arial", Font.PLAIN, 12 );
if ( font == null )
{
font = new Font( "SansSerif", Font.PLAIN, 12 );
}
Keyword.font = font;
}

Why does Javac complain that font is declared final?
It is only assigned a value once.

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;
}

Jim
 
M

Michael Rauscher

Hi Roedy,

Roedy said:
private static final Font font;
static {
Font font = new Font( "Arial", Font.PLAIN, 12 );
if ( font == null )
{
font = new Font( "SansSerif", Font.PLAIN, 12 );
}
Keyword.font = font;
}

Why does Javac complain that font is declared final?
It is only assigned a value once.

I'd say because you re-declared it within your static block, try

private static final Font font;
static {
font = new Font...

Bye
Michael
 
X

xarax

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
}
}
 
J

Jim

Jim said:
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; // This won't work either
}

Jim

Missed something. [Blush]

public class Keyword {
private static final Font font;
static {
Keyword.font = null; // won't compile
}
}

But

public class Keyword {
private static final Font font;
static {
font = null; // Will compile
}
}

I can't find exact wording that says so, but it seems that the
compiler cannot figure out if the qualified field name Keyword.font
is "definitely unassigned" when we attempt to assign a value to it. When
using the field name font, the compiler seems to be able to make that
determination.

What I can't find is exact wording makes a difference between a
qualified and unqualified "blank" static final. Well it looks like
I've found out "what" also but not exactly why.

Jim
 
R

Roedy Green

I'd say because you re-declared it within your static block, try

private static final Font font;
static {
font = new Font...

I the Font font is a local and should not confuse it.

What seems to be confusing it in the notation Keyword.font rather than
plain font. It is the same static variable. I think it is minor
compiler bug.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top