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. Advertisements

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:
    5,781
    Thomas Weidenfeller
    Oct 12, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,383
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,853
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    5
    Views:
    750
    Chris Uppal
    Nov 17, 2006
  5. Reporter
    Replies:
    3
    Views:
    679
    Mike Schilling
    May 12, 2007
  6. sasha
    Replies:
    3
    Views:
    390
    Abhijat Vatsyayan
    Jul 29, 2008
  7. puzzlecracker

    anonymous class questions - why final?

    puzzlecracker, Jul 29, 2008, in forum: Java
    Replies:
    0
    Views:
    274
    puzzlecracker
    Jul 29, 2008
  8. Replies:
    1
    Views:
    376
Loading...