Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is final parameter stored in anonymous class instance?

I have the following static factory method that creates a list view out of an int array:

public static List<Integer> newInstance(final int[] numbers) {
    return new AbstractList<Integer>() {

        @Override
        public Integer get(int index) {
            return numbers[index];
        }

        @Override
        public int size() {
            return numbers.length;
        }
    };
}


public static void main(String[] args) {
    int[] sequence = {10, 20, 30};
    List<Integer> list = ListFactory.newInstance(sequence);
    System.out.println("List is "+list);

}

In "Effective Java", Joshua Bloch mentioned this

as an Adapter that allows an int array to be viewed as a list of Integer instances.

However, I remember that Adapter uses composition and the instance of the anonymous list implementation should use the int[] as a member field.

Where exactly is the int[] input parameter stored if it's not a member field of the anonymous list implementation?

I would appreciate if anyone could provide some insights or some links to look for more information.

like image 999
Kewei Shang Avatar asked Dec 15 '15 13:12

Kewei Shang


People also ask

What is the purpose of anonymous class in Java?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Can anonymous class be passed as an argument?

Anonymous inner classes can be defined not just within a method, but even within an argument to a method. Anonymous inner classes cannot have explicit constructors declared because they have no name to give the constructor.

What is the inner and anonymous inner class?

It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.

How do you declare an anonymous class in Java?

Object = new Example() { public void display() { System. out. println("Anonymous class overrides the method display()."); } }; Here, an object of the anonymous class is created dynamically when we need to override the display() method.


1 Answers

You can use javac -d . -XD-printflat ListFactory.java to see how the compiler understands the inner class. Actually, there are two Java classes in your example. The ListFactory (note how numbers are passed to the constructor of ListFactory$1):

public class ListFactory {

    public ListFactory() {
        super();
    }

    public static List newInstance(final int[] numbers) {
        return new ListFactory$1(numbers);
    }
}

and the representation of the anonymous implementation of AbstractList:

class ListFactory$1 extends AbstractList {
    /*synthetic*/ final int[] val$numbers;

    ListFactory$1(/*synthetic*/ final int[] val$numbers) {
        this.val$numbers = val$numbers;
        super();
    }

    @Override()
    public Integer get(int index) {
        return Integer.valueOf(val$numbers[index]);
    }

    @Override()
    public int size() {
        return val$numbers.length;
    }

    @Override()
    /*synthetic*/ public Object get(/*synthetic*/ int index) {
        return this.get(index);
    }
}

The methods and fields marked as synthetic are generated by the compiler and not accessible to you as a programmer, but are used during runtime to access the int array. And indeed there is a val$numbers field that holds a final reference to the int array.

By the way you can also notice boxing from int to Integer in Integer get(int index) and that to comply with the raw (non-generic) List interface an extra Object get(int index) method is generated that delegates to the type-safe Integer get(int index) implementation.

like image 129
Adam Michalik Avatar answered Oct 13 '22 01:10

Adam Michalik