Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: Does final serve any purpose? (http://www.velocityreviews.com/forums/t735418-re-does-final-serve-any-purpose.html)

Tom Anderson 10-13-2010 09:59 PM

Re: Does final serve any purpose?
 
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 10-14-2010 11:45 AM

Re: Does final serve any purpose?
 
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

Lew 10-14-2010 12:43 PM

Re: Does final serve any purpose?
 
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 10-14-2010 11:17 PM

Re: Does final serve any purpose?
 
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 10-15-2010 03:37 AM

Re: Does final serve any purpose?
 
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


All times are GMT. The time now is 09:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.