public
class
Bat {
final
double
PI
= 3.14;
//
在定义时赋值
final
int
i
;
//
因为要在构造函数中进行初始化
,
所以此处便不可再赋值
final
List<Bat>
list
;
//
因为要在构造函数中进行初始化
,
所以此处便不可再赋值
Bat() {
i
= 100;
list
=
new
LinkedList<Bat>();
}
Bat(
int
ii, List<Bat> l) {
i
= ii;
list
= l;
}
public
static
void
main(String[] args) {
Bat b =
new
Bat();
b.
list
.add(
new
Bat());
// b.i=25;
// b.list=new ArrayList<Bat>();
System.
out
.println(
"I="
+ b.
i
+
" List Type:"
+ b.
list
.getClass());
b =
new
Bat(23,
new
ArrayList<Bat>());
b.
list
.add(
new
Bat());
System.
out
.println(
"I="
+ b.
i
+
" List Type:"
+ b.
list
.getClass());
}
}
|
class
Thingie {
public
static
Thingie getDefaultThingie() {
return
new
Thingie();
}
}
public
class
Foo {
private
final
Thingie
thingie
;
public
Foo() {
try
{
thingie
=
new
Thingie();
}
catch
(Exception e) {
thingie
= Thingie.getDefaultThingie();
//Error:The final field thingie may already have been assigned
}
}
}
|
public
class
Foo {
private
final
Thingie
thingie
;
public
Foo() {
Thingie tempThingie;
try
{
tempThingie =
new
Thingie();
}
catch
(Exception e) {
tempThingie = Thingie.getDefaultThingie();
}
thingie
= tempThingie;
}
}
|
private
final
int
VAL_ONE
=9;
private
static
final
int
VAL_TWO
=99;
public
static
final
int
VAL_THREE
=999;
|
class
Value {
int
i
;
public
Value(
int
i) {
this
.
i
= i;
}
}
public
class
FinalData {
private
static
Random
rand
=
new
Random();
private
String
id
;
public
FinalData(String id) {
this
.
id
= id;
}
private
final
int
i4
=
rand
.nextInt(20);
static
final
int
i5
=
rand
.nextInt(20);
public
String toString() {
return
id
+
":"
+
"i4:"
+
i4
+
", i5="
+
i5
;
}
public
static
void
main(String[] args) {
FinalData fd1 =
new
FinalData(
"fd1"
);
System.
out
.println(fd1);
System.
out
.println(
"Creating new FinalData"
);
FinalData fd2 =
new
FinalData(
"fd2"
);
System.
out
.println(fd1);
System.
out
.println(fd2);
}
}
|
class
Value {
int
i
;
public
Value(
int
i) {
this
.
i
= i;
}
}
public
class
… {
private
Value
v1
=
new
Value(11);
private
final
Value
v2
=
new
Value(22);
private
static
final
Value
v3
=
new
Value(33);
…
}
public
static
void
main(String[] args) {
…
fd1.
v2
.
i
++;
// OK--Object isn't constant!
fd1.
v1
=
new
Value(9);
//OK--not final
fd1.
v2
=
new
Value(0);
//Error:Can't change reference
fd1.
v3
=
new
Value(1);
//Error:Can't change reference
…
}
|
public
class
… {
private
final
int
[]
a
={1,2,3,4,5,6};
…
}
public
static
void
main(String[] args) {
…
for
(
int
i=0;i<fd1.
a
.
length
;i++)
fd1.
a
[i]++;
// OK--Object isn't constant!
fd1.
a
=
new
int
[3];
//Error:Can't change reference
…
}
|
// Not immutable -- the states array could be modified by a malicious
// callerpublic
class
DangerousStates {
private
final
String[]
states
=
new
String[] {
" Alabama "
,
" Alaska "
,
"ect"
};
public
String[] getStates() {
return
states
;
}
}
|
// Immutable -- returns an unmodifiable List insteadpublic
class
SafeStates {
private
final
String[]
states
=
new
String[] {
" Alabama "
,
" Alaska "
,
"ect"
};
private
final
List
statesAsList
=
new
AbstractList() {
public
Object get(
int
n) {
return
states
[n];
}
public
int
size() {
return
states
.
length
;
}
};
public
List getStates() {
return
statesAsList
;
}
}
|
public
class
INClass {
void
innerClass(
final
String str) {
class
IClass {
IClass() {
System.
out
.println(str);
}
}
IClass ic =
new
IClass();
}
public
static
void
main(String[] args) {
INClass inc =
new
INClass();
inc.innerClass(
"Hello"
);
}
}
|