Home  >  Article  >  类库下载  >  Java final argument

Java final argument

高洛峰
高洛峰Original
2016-11-02 15:06:041700browse

Java 1.1 allows us to make arguments final by declaring them appropriately in the argument list. This means that inside a method, we cannot change what the argument handle points to. As shown below:

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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:java interfaceNext article:java interface