将类变量引用到局部变量

tom*_*omi 1 java

为什么有些人在方法中对字段变量进行新的引用?

public class Foo {
    int mFoo[] = {1, 2, 3};

    void method() {
        int foo[] = mFoo; // WHY not just use mFoo?

        print(foo[0]); // WHY not just use mFoo?
    }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*noy 5

例如,如果在代码的其他位置更改mFoo的值,则可能是个好主意.特别是在多线程环境中,您需要确保仍在处理正确的数据.