💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### [final 参数](https://lingcoder.gitee.io/onjava8/#/book/08-Reuse?id=final-%e5%8f%82%e6%95%b0) 在参数列表中,将参数声明为 final 意味着在方法中不能改变参数指向的对象或基本变量: ~~~ // reuse/FinalArguments.java // Using "final" with method arguments class Gizmo { public void spin() { } } public class FinalArguments { void with(final Gizmo g) { //-g = new Gizmo(); // Illegal -- g is final } void without(Gizmo g) { g = new Gizmo(); // OK -- g is 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); } } ~~~ 方法`f()`和`g()`展示了**final**基本类型参数的使用情况。你只能读取而不能修改参数。这个特性主要用于传递数据给匿名内部类。这将在”内部类“章节中详解。