How does one increment a final variable?

Discussion in 'Java' started by Oliver Wong, Jan 27, 2006.

  1. Oliver Wong

    Oliver Wong Guest

    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
     
    Oliver Wong, Jan 27, 2006
    #1
    1. Advertising

  2. > 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?)

    --
    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
     
    Andrey Kuznetsov, Jan 27, 2006
    #2
    1. Advertising

  3. Oliver Wong

    Oliver Wong Guest

    "Andrey Kuznetsov" <> wrote in message
    news:dre6r3$rn8$...
    >> 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?)


    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, Jan 27, 2006
    #3
  4. Oliver Wong

    Oliver Wong Guest

    "Oliver Wong" <> wrote in message
    news:WdxCf.112423$m05.82916@clgrps12...

    > <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
     
    Oliver Wong, Jan 27, 2006
    #4
  5. >> 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.

    --
    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
     
    Andrey Kuznetsov, Jan 27, 2006
    #5
  6. Oliver Wong wrote:
    > 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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Jan 27, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    104
    Views:
    11,039
    Jordan Abel
    Oct 28, 2005
  2. Replies:
    99
    Views:
    2,516
    eliza81
    Jun 11, 2010
  3. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    904
    Alf P. Steinbach /Usenet
    May 22, 2011
  4. Summercool
    Replies:
    4
    Views:
    85
    Xavier Noria
    Sep 11, 2007
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    540
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page