Skip to content Skip to sidebar Skip to footer

Why Final Is Used In Method Parameters

When am coding android I came across the following thing @Override public View getView(final int position, View convertView, ViewGroup parent) { convertView = _inflat

Solution 1:

final is used in method parameters to make the referencesunchangeable 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

Solution 2:

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

Solution 3:

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

Solution 4:

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.

Solution 5:

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

Post a Comment for "Why Final Is Used In Method Parameters"