-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalData.java
More file actions
59 lines (51 loc) · 1.62 KB
/
FinalData.java
File metadata and controls
59 lines (51 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//: reusing/FinalData.java
// The effect of final on fields
package com.reusing07;
import java.util.Random;
class Value {
int i; // Package access
public Value(int i) {
this.i = i;
}
}
public class FinalData {
private static Random rand = new Random(50);
private String id;
public FinalData(String id) { this.id = id; }
// Can be compile-time constants:
private final int valueOne = 9;
private static final int VALUE_TWO = 99;
// Typical public constant
public static final int VALUE_THREE = 39;
// Cannot be compile-time constants
private final int i4 = rand.nextInt(20);
static final int INT_5 = rand.nextInt(20);
private Value v1 = new Value(11);
private final Value v2 = new Value(22);
private static final Value v3 = new Value(33);
// Arrays:
private final int[] a = { 1, 2, 3, 4, 5, 6 };
public String toString() {
return id + ": " + "i4 = " + i4 + ", INT_5 = " + INT_5;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// final 基本类型,数据无法改变
// final 对象(数组也是对象),不能改变引用
// final static final 类第一次初始化,后面的类对象初始化值也不变
FinalData fd1 = new FinalData("fd1");
//! fd1.valueOne++; // Error: can't change value
fd1.v2.i++; // Object is not constant!
fd1.v1 = new Value(9); // OK -- not final
for(int i = 0; i < fd1.a.length; i++) {
fd1.a[i]++; // Object is not constants
}
//! fd1.v2 = new Value(0); // change reference
//! fd1.a = new int[3];
System.out.println(fd1);
System.out.println("Creating new FinalData");
FinalData fd2 = new FinalData("fd2");
System.out.println(fd1);
System.out.println(fd2);
}
}