Re: Does final serve any purpose?

Discussion in 'Java' started by Tom Anderson, Oct 13, 2010.

  1. Tom Anderson

    Tom Anderson Guest

    On Wed, 13 Oct 2010, Spud wrote:

    > Does the final keyword serve any purpose, except to make constants constant?


    Does the type of a variable serve any purpose?

    final, like types, doesn't let you do anything you couldn't do without it.
    Rather, it stops you doing things you didn't mean to do. That's it.

    If you find you don't tend to do things you didn't mean to do, then you
    won't find final very useful. But if you find that you sometimes do, or,
    more likely, if some other unspeakable scoundrel who works on your
    codebase does, then perhaps you might.

    tom

    --
    Feeling down? Talk to someone you hate and replace that low feeling with
    pure rage!! -- Rhys Davies
    Tom Anderson, Oct 13, 2010
    #1
    1. Advertising

  2. Tom Anderson

    Tom Anderson Guest

    On Wed, 13 Oct 2010, Peter Duniho wrote:

    > Tom Anderson wrote:
    >> On Wed, 13 Oct 2010, Spud wrote:
    >>
    >>> Does the final keyword serve any purpose, except to make constants
    >>> constant?

    >>
    >> Does the type of a variable serve any purpose?
    >>
    >> final, like types, doesn't let you do anything you couldn't do without it.

    >
    > I suppose that depends on your definition of "couldn't do without it".
    >
    > The "final" keyword is required in order to use local variables in anonymous
    > classes.


    True, i'd forgotten about that. And about the fact that it guarantees
    cross-thread visibility of assignments in constructors. Both of those are
    uses of a different (or additional) character to the kind i was thinking
    of.

    >> If you find you don't tend to do things you didn't mean to do, then you
    >> won't find final very useful. But if you find that you sometimes do,
    >> or, more likely, if some other unspeakable scoundrel who works on your
    >> codebase does, then perhaps you might.

    >
    > Thankfully, it doesn't happen often. But I've certainly been known to
    > do things I didn't mean to do, and I consider myself one of the more
    > careful programmers. I like being able to mark something as "final"
    > when appropriate.


    Agreed; i like final myself, although not as much as some people. I use it
    a lot for fields, but not so much for locals.

    tom

    --
    you can't feel your stomack with glory -- Czako
    Tom Anderson, Oct 14, 2010
    #2
    1. Advertising

  3. Tom Anderson

    Lew Guest

    Peter Duniho wrote:
    >> The "final" keyword is required in order to use local variables in
    >> anonymous classes.


    inner classes generally

    Tom Anderson wrote:
    > True, i'd forgotten about that. And about the fact that it guarantees
    > cross-thread visibility of assignments in constructors. Both of those
    > are uses of a different (or additional) character to the kind i was
    > thinking of.

    .. . .
    > Agreed; i like final myself, although not as much as some people. I use
    > it a lot for fields, but not so much for locals.


    --
    Lew
    Lew, Oct 14, 2010
    #3
  4. Tom Anderson

    Lew Guest

    On 10/14/2010 11:16 AM,
    >
    >> Peter Duniho wrote:
    >>>> The "final" keyword is required in order to use local variables in
    >>>> anonymous classes.


    Lew wrote:
    >> inner classes generally


    Peter Duniho wrote:
    > All anonymous classes are inner classes. Not all inner classes are
    > anonymous.


    True but not relevant.

    > I don't know why you're making the distinction, but in the context of


    Because the statement was made about use of local variables in anonymous
    classes without mentioning the non-anonymous inner classes where the rule applies.

    > this discussion it doesn't seem like "inner classes" is nearly as
    > relevant as "anonymous classes". You can't refer to local variables in
    > all inner classes, just the anonymous or local ones.


    Yes, and local ones are not anonymous, therefore it's not enough to discuss
    just anonymous classes. The JLS states the rule as, "Any local variable,
    formal method parameter or exception handler parameter used but not declared
    in an inner class must be declared final."

    My point was that the rule is not limited to anonymous classes as was first
    stated here.

    --
    Lew
    Lew, Oct 15, 2010
    #4
  5. Tom Anderson

    Lew Guest

    On 10/14/2010 09:15 PM, Peter Duniho wrote:
    > Lew wrote:
    >> [...]
    >> Because the statement was made about use of local variables in
    >> anonymous classes without mentioning the non-anonymous inner classes
    >> where the rule applies.

    >
    > And your statement was about "inner classes generally", not restricted


    I was merely echoing the language in the JLS. Complain to them.

    > to just those in methods. If you're going to nitpick, your statement is
    > no more accurate than mine, because "final" doesn't help you use local
    > variables in non-local inner classes at all.


    If you have a problem with calling the rule one for inner classes, take it up
    with Oracle - it's their JLS and that's where the rule is stated that it
    applies to inner classes. Naturally you are correct to say that that only
    means the variety that can see local variables, thus only anonymous and local
    classes.

    > But really, who cares?


    Anyone who wants to be a better Java programmer.

    >> My point was that the rule is not limited to anonymous classes as was
    >> first stated here.

    >
    > My statement never said it was _limited_ to anonymous classes. Simply
    > that that was an example of an important use for "final".
    > Please stop putting words in my mouth.


    Actually, I didn't put words in your mouth. I concluded based on the words
    that you did use, primarily your focus on anonymous classes and your rejection
    of the notion that the rule applied to inner classes more generally.

    You said:
    > it doesn't seem like "inner classes" is nearly as relevant as "anonymous classes".


    The words "anonymous classes" were not words that I put in your mouth.

    I acknowledge that I was led astray by your focus on anonymous classes and
    your characterization of anonymous classes as the rule in your followup
    emails, concomitant to your rejection of the JLS's language specifying "inner
    classes". Sorry.

    --
    Lew
    Lew, Oct 15, 2010
    #5
    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. Shawn
    Replies:
    2
    Views:
    567
    Shawn
    Jun 24, 2005
  2. JFCM
    Replies:
    4
    Views:
    5,720
  3. Hattuari
    Replies:
    11
    Views:
    823
    Julie J.
    Feb 26, 2004
  4. Kevin McMurtrie

    Re: Does final serve any purpose?

    Kevin McMurtrie, Oct 14, 2010, in forum: Java
    Replies:
    4
    Views:
    267
    Kevin McMurtrie
    Oct 15, 2010
  5. Peter
    Replies:
    8
    Views:
    308
    Jorgen Grahn
    Dec 29, 2012
Loading...

Share This Page