0

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;

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.

ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
ARP
  • 603
  • 1
  • 11
  • 23

6 Answers6

11

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

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
5

You are using position in your annonymous inner class. Hence it is required that position be final.

An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.

The final modifier indicates that the value of this field cannot change.

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

We use final keyword with method parameters in order to prevent the code inside the method to modify the value of that parameter.

jagmohan
  • 2,052
  • 2
  • 26
  • 41
0

final makes your variable constant.

As your onclick method is going to get called after the finish of this function and your variable wont exist anymore so when you use a final variable it's constant value is passed inside onclick.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0
  • A java variable can be declared using the keyword final. Then the final variable can be assigned only once.

  • A variable that is declared as final and not initialized is called a blank final variable. A blank final variable forces the constructors to initialise it.

  • Java classes declared as final cannot be extended. Restricting inheritance!

  • Methods declared as final cannot be overridden. In methods private is equal to final, but in variables it is not.

  • final parameters – values of the parameters cannot be changed after initialization. Do a small java exercise to find out the implications of final parameters in method overriding.

  • Java local classes can only reference local variables and parameters that are declared as final.

  • A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.

For more information read this

also see Use of final keyword in Java method performance?

Community
  • 1
  • 1
rachana
  • 3,344
  • 7
  • 30
  • 49
0

Its because getView is called when your listView is inflating layout... And onClick is called when your view (move) is pressed... At that time when its onClick is triggered, the variable position in getView is destroyed already... By making it final you ensure that the value of position is preserved for later use

Rehan
  • 3,270
  • 5
  • 31
  • 30