为什么在方法参数中使用Final

为什么在方法参数中使用Final

本文介绍了为什么在方法参数中使用Final的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编码android时,我遇到了以下问题

When am coding android I came across the following thing

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = _inflater.inflate(R.layout.abstract_list_row_item, null);
        move.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // if btn is clcked then data is changed an we need to refresh framents
                    MainAppDataController.getInstance().setIsDataChanged(true);
                    callWhiteListDB = new CallWhiteListDB(_context);
                    callWhiteListDB.openToWrite();
                    callWhiteListDB.insert(allContacts.get(position).name, allContacts.get(position).number);
                    callWhiteListDB.close();

                    callBlackListDB = new CallBlackListDB(_context);
                    callBlackListDB.openToWrite();
                    callBlackListDB.deleteSingleItem(allContacts.get(position).dbId);
                    callBlackListDB.close();
                    populateList(position);
                    notifyListView(view);
                }
            });
        return convertView;

在上面的示例中, getView()方法具有诸如int position,View convertView,ViewGroup parent之类的参数.代码>,Eclipse抛出编译错误并要求将位置确定为最终位置.我为什么要入围决赛?AFAIK final用于常量.

In the above example getView() method has parameters like int position, View convertView, ViewGroup parent.My Observation was as soon as I start using position variable inside onclick(), Eclipse throws compilation error and asks make position as final. Why should I make it as final? AFAIK final is used for constants.

推荐答案

final 用于方法参数中,以创建引用 不可更改,然后将其传递给方法.这是保护传递的参数的一种特殊方式.因此,接收方法将无法使用新对象或新值重新初始化

final is used in method parameters to make the references unchangeable after it is passed into the method. This is a specialized way of securing the passed parameters. so, the method receiving will not be able to re-initialize it with new object or value

这篇关于为什么在方法参数中使用Final的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:04