为什么要“将myVariable转换为最终的一个元素数组"?

为什么要“将myVariable转换为最终的一个元素数组

问题描述:

我正在尝试访问内部类中的 h 变量,但始终出现错误无法为最终变量 h 赋值".我尝试了快速修复,它指示我将h转换为最终的一个元素数组".这是什么意思?

I'm trying to access the h variable in the inner class but an error keeps showing up "Cannot assign a value to final variable h". I tried quick-fix and it instructed me to "Transform h to final one element array".What does that mean?

int Update ()
{
    final int h;
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    preferences.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {


        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

            if(key.equalsIgnoreCase("PINCODE"))
            {
                h = sharedPreferences.getInt(key,0);

            }
        }
    });

    return h;
}

}

在内部类中,您不能将值分配给在封闭类中某处声明的局部变量(本身),但是您可以可以更改所引用对象的状态(调用方法,设置器等)(如果变量指向某个对象而不是原始类型).数组就是对象.

From the inner class you can't assign value to a local variable (itself) declared somewhere in the enclosing class, but you can change state (call methods, setters, ...) of the referenced object (if the variable points to some object and not to a primitive type). And array is object.

检查Java语言规范-关于

Check Java language specification - section about inner classes.