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

Java final argument

高洛峰
Release: 2016-11-02 15:06:04
Original
1872 people have browsed it

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


Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template