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() {
    }
}

 

相关文章:

  • 2021-12-04
  • 2021-12-01
  • 2022-12-23
  • 2021-06-10
  • 2022-02-28
  • 2021-04-10
  • 2022-12-23
  • 2021-12-03
猜你喜欢
  • 2022-02-07
  • 2021-08-27
  • 2021-07-01
  • 2021-06-16
  • 2022-12-23
  • 2021-06-09
  • 2021-06-30
相关资源
相似解决方案