final class

Y

Yin99

If you make a class final at the top, does the compiler automatically
make every field and method in the class final as well?

(I'm wondering with a final class, is it completely redundant to go
through every declared variable and method adding the final keyword as
well?)

Thanks, Yin99
 
M

Michiel Konstapel

If you make a class final at the top, does the compiler automatically
make every field and method in the class final as well?

No, declaring a class final only means it cannot be subclassed. Of course,
this means all methods are effectively final as well, but it does nothing
to the class's fields.
HTH,
Michiel
 
K

klynn47

The final keyword has different contexts. When you declare a class
definition final, it just means that you can't make a subclass of the
class. It does not mean that the variables in the class definition are
final.
 
Y

Yin99

I read somewhere that using the final keyword on a method (and/or
variable) can make the code more efficient because the compiler can
'inline' the final compiled version (I'm shaky on technical details.)
Is there any truth to this? Would I get more efficient code (at the
low-level after compiled) by making a class final, or going through
making methods or variables final?
 
G

Gerbrand van Dieijen

Michiel Konstapel schreef:
No, declaring a class final only means it cannot be subclassed. Of
course, this means all methods are effectively final as well, but it
does nothing to the class's fields.
HTH,
Michiel

final fields would be 'read-only'.
if you declare
final int s=123
you can't change s anymore
(by the way if it is referencing an object, the object itself isn't
readonly automatically)
 
A

Adam Maass

Yin99 said:
I read somewhere that using the final keyword on a method (and/or
variable) can make the code more efficient because the compiler can
'inline' the final compiled version (I'm shaky on technical details.)
Is there any truth to this?

There might be some truth to this, depending on the characteristics of your
program and the particular version of the particular JVM you're using. Some
JVMs have an abundance of command-line arguments that might affect behavior
also.

A final method is somewhat easier for the JVM to inline than a non-final
method, but that does not mean that non-final methods won't be inlined, nor
does it mean that final methods will be.


So, in general: use the 'final' keyword for its semantic meanings:

A) On a class, it means that the class cannot be subclassed. (And
implicitly, that its methods are final.)
B) On a method, it means that the method cannot be overridden.
C) On a variable, it means that the variable's value cannot change (once the
variable has been initialized).


Use the 'final' keyword for performance reasons if and only if you can prove
that it actually improves performance in your particular set of
circumstances.


-- Adam Maass
 
T

thufir

IIRC there's a connection between final fields, static factory methods
and singletons. those pattern goes hand in hand with final fields?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top