<s>
In	O
the	O
Java	B-Language
programming	I-Language
language	I-Language
,	O
the	O
final	O
keyword	O
is	O
used	O
in	O
several	O
contexts	O
to	O
define	O
an	O
entity	O
that	O
can	O
only	O
be	O
assigned	O
once	O
.	O
</s>
<s>
As	O
doing	O
this	O
can	O
confer	O
security	O
and	O
efficiency	O
benefits	O
,	O
many	O
of	O
the	O
Java	B-Language
standard	O
library	O
classes	O
are	O
final	O
,	O
such	O
as	O
and	O
.	O
</s>
<s>
A	O
final	B-Language
method	I-Language
cannot	O
be	O
overridden	B-Language
or	O
hidden	O
by	O
subclasses	O
.	O
</s>
<s>
This	O
is	O
used	O
to	O
prevent	O
unexpected	O
behavior	O
from	O
a	O
subclass	O
altering	O
a	O
method	B-Language
that	O
may	O
be	O
crucial	O
to	O
the	O
function	O
or	O
consistency	O
of	O
the	O
class	O
.	O
</s>
<s>
A	O
common	O
misconception	O
is	O
that	O
declaring	O
a	O
method	B-Language
as	O
final	O
improves	O
efficiency	O
by	O
allowing	O
the	O
compiler	O
to	O
directly	O
insert	O
the	O
method	B-Language
wherever	O
it	O
is	O
called	O
(	O
see	O
inline	O
expansion	O
)	O
.	O
</s>
<s>
Because	O
the	O
method	B-Language
is	O
loaded	O
at	B-Library
runtime	I-Library
,	O
compilers	O
are	O
unable	O
to	O
do	O
this	O
.	O
</s>
<s>
Only	O
the	O
runtime	B-Library
environment	O
and	O
JIT	O
compiler	O
know	O
exactly	O
which	O
classes	O
have	O
been	O
loaded	O
,	O
and	O
so	O
only	O
they	O
are	O
able	O
to	O
make	O
decisions	O
about	O
when	O
to	O
inline	O
,	O
whether	O
or	O
not	O
the	O
method	B-Language
is	O
final	O
.	O
</s>
<s>
Machine	B-Language
code	I-Language
compilers	O
that	O
generate	O
directly	O
executable	O
,	O
platform-specific	O
machine	B-Language
code	I-Language
,	O
are	O
an	O
exception	O
.	O
</s>
<s>
When	O
using	O
static	B-Library
linking	I-Library
,	O
the	O
compiler	O
can	O
safely	O
assume	O
that	O
methods	O
and	O
variables	O
computable	O
at	O
compile-time	B-Application
may	O
be	O
inlined	O
.	O
</s>
<s>
A	O
blank	O
final	O
instance	B-Application
variable	I-Application
of	O
a	O
class	O
must	O
be	O
definitely	O
assigned	O
in	O
every	O
constructor	O
of	O
the	O
class	O
in	O
which	O
it	O
is	O
declared	O
;	O
similarly	O
,	O
a	O
blank	O
final	O
static	O
variable	O
must	O
be	O
definitely	O
assigned	O
in	O
a	O
static	O
initializer	O
of	O
the	O
class	O
in	O
which	O
it	O
is	O
declared	O
;	O
otherwise	O
,	O
a	O
compile-time	B-Application
error	I-Application
occurs	O
in	O
both	O
cases	O
.	O
</s>
<s>
But	O
the	O
object	O
that	O
it	O
references	O
is	O
still	O
mutable	B-Application
,	O
if	O
it	O
was	O
originally	O
mutable	B-Application
.	O
)	O
</s>
<s>
Unlike	O
the	O
value	O
of	O
a	O
constant	O
,	O
the	O
value	O
of	O
a	O
final	O
variable	O
is	O
not	O
necessarily	O
known	O
at	O
compile	B-Application
time	I-Application
.	O
</s>
<s>
To	O
illustrate	O
that	O
finality	O
does	O
n't	O
guarantee	O
immutability	B-Application
:	O
suppose	O
we	O
replace	O
the	O
three	O
position	O
variables	O
with	O
a	O
single	O
one	O
:	O
</s>
<s>
Like	O
full	O
immutability	B-Application
,	O
the	O
use	O
of	O
final	O
variables	O
has	O
great	O
advantages	O
,	O
especially	O
in	O
optimization	O
.	O
</s>
<s>
Final	O
variables	O
can	O
be	O
used	O
to	O
construct	O
trees	O
of	O
immutable	B-Application
objects	I-Application
.	O
</s>
<s>
To	O
achieve	O
this	O
,	O
an	O
immutable	B-Application
class	I-Application
must	O
only	O
have	O
final	O
fields	O
,	O
and	O
these	O
final	O
fields	O
may	O
only	O
have	O
immutable	B-Application
types	O
themselves	O
.	O
</s>
<s>
Java	B-Language
's	O
primitive	O
types	O
are	O
immutable	B-Application
,	O
as	O
are	O
strings	O
and	O
several	O
other	O
classes	O
.	O
</s>
<s>
If	O
the	O
above	O
construction	O
is	O
violated	O
by	O
having	O
an	O
object	O
in	O
the	O
tree	O
that	O
is	O
not	O
immutable	B-Application
,	O
the	O
expectation	O
does	O
not	O
hold	O
that	O
anything	O
reachable	O
via	O
the	O
final	O
variable	O
is	O
constant	O
.	O
</s>
<s>
The	O
origin	O
is	O
implemented	O
using	O
a	O
java.awt.Point	O
though	O
,	O
and	O
this	O
class	O
defines	O
its	O
fields	O
as	O
public	O
and	O
modifiable	O
.	O
</s>
<s>
To	O
prevent	O
this	O
undesirable	O
situation	O
,	O
a	O
common	O
requirement	O
is	O
that	O
all	O
fields	O
of	O
an	O
immutable	B-Application
object	I-Application
must	O
be	O
final	O
,	O
and	O
that	O
the	O
types	O
of	O
these	O
fields	O
must	O
be	O
immutable	B-Application
themselves	O
.	O
</s>
<s>
This	O
disqualifies	O
java.util.Date	O
and	O
java.awt.Point	O
and	O
several	O
other	O
classes	O
from	O
being	O
used	O
in	O
such	O
immutable	B-Application
objects	I-Application
.	O
</s>
<s>
When	O
an	O
anonymous	B-Language
inner	I-Language
class	I-Language
is	O
defined	O
within	O
the	O
body	O
of	O
a	O
method	B-Language
,	O
all	O
variables	O
declared	O
final	O
in	O
the	O
scope	O
of	O
that	O
method	B-Language
are	O
accessible	O
from	O
within	O
the	O
inner	B-Language
class	I-Language
.	O
</s>
<s>
This	O
allows	O
the	O
Java	B-Language
compiler	O
to	O
"	O
capture	O
"	O
the	O
value	O
of	O
the	O
variable	O
at	O
run-time	O
and	O
store	O
a	O
copy	O
as	O
a	O
field	O
in	O
the	O
inner	B-Language
class	I-Language
.	O
</s>
<s>
Once	O
the	O
outer	O
method	B-Language
has	O
terminated	O
and	O
its	O
stack	B-General_Concept
frame	I-General_Concept
has	O
been	O
removed	O
,	O
the	O
original	O
variable	O
is	O
gone	O
but	O
the	O
inner	B-Language
class	I-Language
's	O
private	O
copy	O
persists	O
in	O
the	O
class	O
's	O
own	O
memory	O
.	O
</s>
<s>
The	O
blank	O
final	O
,	O
which	O
was	O
introduced	O
in	O
Java	B-Language
1.1	O
,	O
is	O
a	O
final	O
variable	O
whose	O
declaration	O
lacks	O
an	O
initializer	O
.	O
</s>
<s>
Previous	O
to	O
Java	B-Language
1.1	O
,	O
a	O
final	O
variable	O
was	O
required	O
to	O
have	O
an	O
initializer	O
.	O
</s>
<s>
In	O
order	O
to	O
do	O
this	O
,	O
a	O
Java	B-Language
compiler	O
runs	O
a	O
flow	O
analysis	O
to	O
ensure	O
that	O
,	O
for	O
every	O
assignment	O
to	O
a	O
blank	O
final	O
variable	O
,	O
the	O
variable	O
is	O
definitely	O
unassigned	O
before	O
the	O
assignment	O
;	O
otherwise	O
a	O
compile-time	B-Application
error	I-Application
occurs	O
.	O
</s>
<s>
In	O
C	B-Language
and	O
C++	B-Language
,	O
the	O
analogous	O
construct	O
is	O
the	O
const	O
keyword	O
.	O
</s>
<s>
This	O
differs	O
substantially	O
from	O
final	O
in	O
Java	B-Language
,	O
most	O
basically	O
in	O
being	O
a	O
type	B-Language
qualifier	I-Language
:	O
const	O
is	O
part	O
of	O
the	O
type	O
,	O
not	O
only	O
part	O
of	O
the	O
identifier	O
(	O
variable	O
)	O
.	O
</s>
<s>
Nonetheless	O
,	O
casting	O
away	O
constness	O
and	O
then	O
modifying	O
the	O
object	O
results	O
in	O
undefined	B-Language
behavior	I-Language
if	O
the	O
object	O
was	O
originally	O
declared	O
const	O
.	O
</s>
<s>
Java	B-Language
's	O
final	O
is	O
a	O
strict	O
rule	O
such	O
that	O
it	O
is	O
impossible	O
to	O
compile	O
code	O
that	O
directly	O
breaks	O
or	O
bypasses	O
the	O
final	O
restrictions	O
.	O
</s>
<s>
This	O
feature	O
is	O
mostly	O
made	O
use	O
of	O
when	O
deserializing	B-Application
objects	O
with	O
final	O
members	O
.	O
</s>
<s>
Further	O
,	O
because	O
C	B-Language
and	O
C++	B-Language
expose	O
pointers	O
and	O
references	O
directly	O
,	O
there	O
is	O
a	O
distinction	O
between	O
whether	O
the	O
pointer	O
itself	O
is	O
constant	O
,	O
and	O
whether	O
the	O
data	O
pointed	O
to	O
by	O
the	O
pointer	O
is	O
constant	O
.	O
</s>
<s>
This	O
usage	O
results	O
in	O
behaviour	O
which	O
mimics	O
the	O
behaviour	O
of	O
a	O
final	O
variable	O
reference	O
in	O
Java	B-Language
.	O
</s>
<s>
C#	O
can	O
be	O
considered	O
as	O
similar	O
to	O
Java	B-Language
,	O
in	O
terms	O
of	O
its	O
language	O
features	O
and	O
basic	O
syntax	O
:	O
Java	B-Language
has	O
JVM	O
,	O
C#	O
has	O
.Net	O
Framework	O
;	O
Java	B-Language
has	O
bytecode	O
,	O
C#	O
has	O
MSIL	O
;	O
Java	B-Language
has	O
no	O
pointers	O
(	O
real	O
memory	O
)	O
support	O
,	O
C#	O
is	O
the	O
same	O
.	O
</s>
<s>
Regarding	O
the	O
final	O
keyword	O
,	O
C#	O
has	O
two	O
related	O
keywords	O
:	O
</s>
<s>
Note	O
that	O
a	O
key	O
difference	O
between	O
the	O
C/C	O
++	O
derived	O
keyword	O
const	O
and	O
the	O
C#	O
keyword	O
readonly	O
is	O
that	O
const	O
is	O
evaluated	O
at	O
compile	B-Application
time	I-Application
,	O
while	O
readonly	O
is	O
evaluated	O
at	B-Library
runtime	I-Library
,	O
and	O
thus	O
can	O
have	O
an	O
expression	O
that	O
is	O
only	O
calculated	O
and	O
fixed	O
later	O
(	O
at	B-Library
runtime	I-Library
)	O
.	O
</s>
