???????Java final????????????
???????????? ???????[ 2015/10/26 13:32:06 ] ??????????????? .NET
????reader() ????????????????:
???????味???????? obj;
???????味???????? obj ???????????? j??
???????味???????? obj ??????? final ?? i??
???????????????写??? A ??蟹????魏??????????????????????
?????????????????????????? i ???????????????????????????????????????????写??? A 写?????????????????????????? final ??????? A ????????????????????????????
?????? final ??????????????????:??????????? final ?????????????????? ??? final ??????????谩??????????????校?????????貌?? null????????? ????? final ?????????? A ????????????
??????? final ????????????
??????? final ?????????????写 final ????????????????????????????????????????
????????????????? final ??????????????写???????????????????????????????????????????????????????????????????????
???????鈥�????????
public class FinalReferenceExample {
final int[] intArray;
static FinalReferenceExample obj;
public FinalReferenceExample() {
intArray = new int[1];// 1
intArray[0] = 1;// 2
}
public static void writerOne() {// A??????
obj = new FinalReferenceExample(); // 3
}
public static void reader() {// 写??? B ???
if (obj != null) { // 4
int temp1 = obj.intArray[0]; // 5
}
}
}
??????????????? A ??? writerOne()??????????????? B ???reader ??????JMM ???????????? B ?????????写??? A ????????卸? final ?????????????写??
?????????????????????????????
???????????锟�?
public class FinalReferenceEscapeExample {
final int i;
static FinalReferenceEscapeExample obj;
public FinalReferenceEscapeExample() {
i = 1;// 1
obj = this;// 2 ???????????????
}
public static void writer() {
new FinalReferenceEscapeExample();
}
public static void reader() {
if (obj != null) {// 3
int temp = obj.i; // 4
}
}
}
?????????????? A ??? writer()?????????????? B ??? reader()??????
???????????? 2 ??????未??????????? B ???????????????? 2 ?????????? ???????????????胁??? 2 ??????? 1 ???妫�??? read()?????????????????? ?????? final ???????????????????????? 1 ????? 2 ?????????????
???????????????????????????????貌???????????? ???????????? final ????????斜?????????????????????????????? ???????? final ?????????????????
??????
???路???
??????????????????
2023/3/23 14:23:39???写?貌??????????
2023/3/22 16:17:39????????????????????些??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???路???????路
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11