How does one increment a final variable?

O

Oliver Wong

I'm reading the JLS 3rd edition and section 15.14.2 has this statement:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.14.2
<quote>
A variable that is declared final cannot be incremented (unless it is a
definitely unassigned (§16) blank final variable (§4.12.4))
</quote>

I did not understand the "unless" part, and I tried various ways to
write code in Eclipse that would increment a final variable (e.g. by not
initializing it, etc.) but nothing I could produce was without compile
errors.

So I'm wondering, what did Sun mean by this statement? Under what
conditions could one increment a final variable?

- Oliver
 
A

Andrey Kuznetsov

A variable that is declared final cannot be incremented (unless it is a
definitely unassigned (§16) blank final variable (§4.12.4))
</quote>

I did not understand the "unless" part, and I tried various ways to
write code in Eclipse that would increment a final variable (e.g. by not
initializing it, etc.) but nothing I could produce was without compile
errors.

So I'm wondering, what did Sun mean by this statement? Under what
conditions could one increment a final variable?

You can assing value to field declared as final in constructor.
int fields are initialized to zero.
So in constructor you _probably_ may increment it so that it become 1.
(isn't this a total contradiction?)
 
O

Oliver Wong

Andrey Kuznetsov said:
You can assing value to field declared as final in constructor.
int fields are initialized to zero.
So in constructor you _probably_ may increment it so that it become 1.
(isn't this a total contradiction?)

That's something I tried, and Eclipse gave me an error.

<SSCCE>
public class Foo {
final int i;
public Foo() {
i++;
}
}
</SSCCE>
<errors>
The final field PostfixExpressionTypeHelper.i cannot be assigned
</errors>

- Oliver
 
O

Oliver Wong

<SSCCE>
public class Foo {
final int i;
public Foo() {
i++;
}
}
</SSCCE>
<errors>
The final field PostfixExpressionTypeHelper.i cannot be assigned
</errors>

Oops, heh heh, you can see that I renamed the class just before posting
it to this group.

- Oliver
 
A

Andrey Kuznetsov

public class Foo {
both compiler and IDEA says "variable i might not have been initialized"
Probably it was just an error in documentation.
 
T

Thomas Hawtin

Oliver said:
I'm reading the JLS 3rd edition and section 15.14.2 has this statement:

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.14.2
<quote>
A variable that is declared final cannot be incremented (unless it is a
definitely unassigned (§16) blank final variable (§4.12.4))
</quote>

I did not understand the "unless" part, and I tried various ways to
write code in Eclipse that would increment a final variable (e.g. by not
initializing it, etc.) but nothing I could produce was without compile
errors.

So I'm wondering, what did Sun mean by this statement? Under what
conditions could one increment a final variable?

It's not in the second edition. It appears to be a (repeated) copy &
paste error from 15.26.

"A variable that is declared final cannot be assigned to (unless it is
a definitely unassigned (§16) blank final variable (§4.12.4)), because
when an access of such a final variable is used as an expression, the
result is a value, not a variable, and so it cannot be used as the first
operand of an assignment operator."

See, copy & paste really is evil.

(FWIW, IIRC you can increment a final variable from the same class in
byte code.)

Tom Hawtin
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top