关键字final有什么用? [英] what is the use of keyword final?

查看:82
本文介绍了关键字final有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,如果我从EditText中删除了关键字final,那么我在将EditText对象(et)传递给意图的第(6)行中遇到了错误.我必须在这里知道final关键字的含义...

In the below code if i remove the keyword final from EditText i am an getting error in the line (6) where i pass EditText object (et) to the intent...I have to knw the significance of final keyword here...

final EditText et=(EditText)findViewById(R.id.t);
        Button b=(Button)findViewById(R.id.b1);
        b.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v)<br>
            {
            Intent on=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+et.getText()));
            startActivity(on);
            }
        });

推荐答案

这是因为您在此处使用了闭包.这意味着内部类使用入站类的上下文.要使用此变量,应将变量声明为final,以免更改.

It is because you use closure here. It means that inner class uses the context of the inbounded one. To use it the variables should be declared final in order not to be changed.

此处中了解更多.

这篇关于关键字final有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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