说说final
final 这个关键字从学Java开始就有提及。它可以用来修饰class, method, method parameter, variable等。这里主要说说最后这一种。对于有final 修饰的variable, 无论是instance variable 还是local variable, 其含义是说(一般)一经初始化和赋值,其值就无法改变。这里,对于instance variable, 我们说,这“初始化和赋值”可能是在声明时就完成的,也可以是在构造函数中完成的。
然而,这个一般无法改变的值却是可以改变的。在JSR 133 中提到了用反射改变final 的instance field 的方法。这里截取的是那里面的例子:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifclass A ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffinal int x;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifA() ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifx = 1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifint f() ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturn d(this,this);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifint d(A a1, A a2) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifint i = a1.x;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifg(a1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifint j = a2.x;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturn j - i;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifstatic void g(A a) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif// uses reflection to change a.x to 2
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页:
[1]