您好,欢迎访问一九零五行业门户网

Java final自变量

java 1.1 允许我们将自变量设成final 属性,方法是在自变量列表中对它们进行适当的声明。这意味着在一个方法的内部,我们不能改变自变量句柄指向的东西。如下所示:
/** * created by xfyou on 2016/11/2. * final自变量演示 */ public class finalarguments { void with(final gizmo g) { //! g = new gizmo(); // illegal -- g is final g.spin(); } void without(gizmo g) { g = new gizmo(); // ok -- g not final g.spin(); } // void f(final int i) { i++; } // can't change // you can only read from a final primitive: int g(final int i) { return i + 1; } public static void main(string[] args) { finalarguments bf = new finalarguments(); bf.without(null); bf.with(null); } } class gizmo { public void spin() { } }
其它类似信息

推荐信息