为什么要“将myVariable转换为最终的一个元素数组"? [英] Why should I "Transform myVariable to final one element array"?

查看:43
本文介绍了为什么要“将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.

这篇关于为什么要“将myVariable转换为最终的一个元素数组"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆