anonymous class questions - why final?

S

sasha

In this example:

static List<Integer> intArrayAsList(final int[] a)
{
if(a==null)
throw new NullPointerException();

return new AbstractList<Integer>(){
public Integer get(int i){
return a;
}
//and other methods like set and size
};
}

I thought that idea of final in the context of anonymous class that
argument passed to it will not be modified outside of it's scope.
However, here this array a can modified elsewhere, say in the
function that called intArrayAsList, defeating the purpose of final to
begin with.

Please elaborate, why does it have to really be final?

Thanks
 
M

Mike Schilling

sasha said:
In this example:

static List<Integer> intArrayAsList(final int[] a)
{
if(a==null)
throw new NullPointerException();

return new AbstractList<Integer>(){
public Integer get(int i){
return a;
}
//and other methods like set and size
};
}

I thought that idea of final in the context of anonymous class that
argument passed to it will not be modified outside of it's scope.


Not exactly: it's that the *reference* can't be modified. This allows
the constrcutor of the anonynmous class to squirrel away the value of
the reference without worrying that code elsewhere in the containing
method might modify it. (Similarly for a scalar, of course.)
 
P

puzzlecracker

In this example:
static List<Integer> intArrayAsList(final int[] a)
{
if(a==null)
throw new NullPointerException();
return new AbstractList<Integer>(){
public Integer get(int i){
return a;
}
//and other methods like set and size
};
}

I thought that idea of final in the context of anonymous class that
argument passed to it will not be modified outside of it's scope.

Well, it's more that it won't be modified after initialization. That's
true even within its scope.
However, here this array a can modified elsewhere, say in the
function that called intArrayAsList, defeating the purpose of final to
begin with.

No. The array itself can be modified, but the variable that's "final"
can't be. _That_ is the point of final.
Please elaborate, why does it have to really be final?

Java doesn't have any way to keep the variable alive after its declaring
scope has exited. So it can only use copies of values in an anonymous
class. It could allow you to use non-final values, but then the question
becomes "what value should it copy?" and "how do you make clear that
changes to the variable later won't affect the anonymous class?"

Instead, the rules are simple: you can only use variables that are
declared "final", so that the compiler knows the value _of the variable_
will never change after initialization and so can be safely copied into
the anonymous class without any ambiguity.

Pete


You're missing the point: there is another reference to this array
elsewhere, which is NOT final. However, the anonymous class thinks
what's provides is in fact final. In other words, say we're passing a
final reference, not a copy, and as such we could mutilate it
unbeknown to the instance of anonymous class.

Thoughts?
 
A

Abhijat Vatsyayan

sasha said:
In this example:

static List<Integer> intArrayAsList(final int[] a)
{
if(a==null)
throw new NullPointerException();

return new AbstractList<Integer>(){
public Integer get(int i){
return a;
}
//and other methods like set and size
};
}

I thought that idea of final in the context of anonymous class that
argument passed to it will not be modified outside of it's scope.
However, here this array a can modified elsewhere, say in the
function that called intArrayAsList, defeating the purpose of final to
begin with.

Please elaborate, why does it have to really be final?

Thanks

Read the third paragraph of section 4.12.4 in the following link -
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.12.4

Abhijat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top