Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

How does one increment a final variable?

Thread view: 
Oliver Wong - 27 Jan 2006 22:04 GMT
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
Andrey Kuznetsov - 27 Jan 2006 22:28 GMT
> A variable that is declared final cannot be incremented (unless it is a
> definitely unassigned (§16) blank final variable (§4.12.4))
[quoted text clipped - 7 lines]
>    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?)

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Oliver Wong - 27 Jan 2006 22:34 GMT
>> A variable that is declared final cannot be incremented (unless it is a
>> definitely unassigned (§16) blank final variable (§4.12.4))
[quoted text clipped - 12 lines]
> 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
Oliver Wong - 27 Jan 2006 22:35 GMT
> <SSCCE>
> public class Foo {
[quoted text clipped - 7 lines]
> 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
Andrey Kuznetsov - 27 Jan 2006 23:03 GMT
>> public class Foo {
>>  final int i;
>>  public Foo() {
>>    i++;
>>  }

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

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Thomas Hawtin - 27 Jan 2006 23:15 GMT
> I'm reading the JLS 3rd edition and section 15.14.2 has this statement:
>
[quoted text clipped - 11 lines]
>     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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.