anonymous class questions - why final?

Discussion in 'Java' started by puzzlecracker, Jul 29, 2008.

  1. 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
    puzzlecracker, Jul 29, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,712
    Thomas Weidenfeller
    Oct 12, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  3. Reporter
    Replies:
    3
    Views:
    465
    Mike Schilling
    May 12, 2007
  4. sasha
    Replies:
    3
    Views:
    296
    Abhijat Vatsyayan
    Jul 29, 2008
  5. puzzlecracker

    anonymous class questions - why final?

    puzzlecracker, Jul 29, 2008, in forum: Java
    Replies:
    0
    Views:
    260
    puzzlecracker
    Jul 29, 2008
Loading...

Share This Page