[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
The Java final keyword.
1. What do the main Java texts/books say about this? Do they (a)
emphasize its existence, (b) provide useful guidelines on its use and,
(c) most importantly, use it frequently in their own sample code so
programmers will imbibe this discipline?
Because I find most Java textbooks depressing, I don't spend a much time with
them (however, a virtual torrent of them crosses my desk, so perhaps quantity
makes up for quality). With that warning in mind, my answers to your questions
are (a) no (not with respect to what the c++ world calls const-ness), (b) most
provide use guidelines, but no rationale or insight (hence the depression), and
(c) no.
2. Do practicing programmers use it much?
Without worrying too much about what you mean by "practicing programmer", I use
them all the time, and the thing I find amazing is the flameage I'll get from
other Java programmers for it. Some of these other programmers are easy to
ignore ("Why is this (a method-local variable) final? It doesn't get
inherited."), others require a bit more work ("You know, the compiler doesn't
optimize finals like this. Why are you doing it?" Fortunately, I can draw on
my experience dealing with C++ programmers about this).
One sensible criticism of const finals, which I haven't yet got, is that
they're misleading, particularly when writing threaded programs: a final
reference to class instance is not a reference to a final class instance. I
catch myself making this mistake at least once per semester (On the other hand,
it pleases me that I can still benefit from all that time I spent fixing into
my tiny mind the difference between const T * and T * const).
And finally, just to make sure this message is up to spec: continuations.