Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous Inner Classes Inside Methods

Please have a look at following code :

import java.util.ArrayList;
import java.util.List;

class Main{
     public static <T> List<T> modifiedList(final List<T> list){
         return new ArrayList<T>(){
            @Override
             public boolean add(T element){
                 super.add(element);
                 return list.add(element);
             }
         };
    }

     public static void main(String[] args) {
         List<String> originalList=new ArrayList<String>();
         List<String> duplicateList=modifiedList(originalList);
         originalList.add("1");
         originalList.add("2");
         originalList.add("3");
         System.out.println(originalList+" "+duplicateList);
         duplicateList.add("4");
         duplicateList.add("5");
         duplicateList.add("6");
         System.out.println(originalList+" "+duplicateList);
     }

In the above code, the instance of an anonymous inner class declared in the method modifiedList() is able to access the parameter passed to that method. AFAIK Java creates a separate bytecode file for inner classes.

Can anyone explain how these local variable bindings are handled by Java at the bytecode level? I mean, how exactly does Java keep track of the reference to the object passed as a parameter to that method?

Any help would be greatly appreciated!

[Sorry for my poor English! If you understand my question, please edit this post and remove the grammatical errors. Thanks!]

like image 410
missingfaktor Avatar asked Dec 23 '09 05:12

missingfaktor


People also ask

Is an anonymous class an inner class?

Anonymous classes are inner classes with no name. As a result, we have to declare and instantiate anonymous classes in a single expression at the point of use. We may either extend an existing class or implement an interface.

Can you create an inner class inside a method?

Method-local Inner Class In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method. A method-local inner class can be instantiated only within the method where the inner class is defined.

Where can you use an anonymous inner class?

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. Tip: Anonymous inner classes are useful in writing implementation classes for listener interfaces in graphics programming.

Can anonymous class have multiple methods?

You can't. The only way to be able to call multiple methods is to assign the anonymous class instance to some variable.


1 Answers

Essentially the code is rewritten by the complier as (note I didn't try to compile it..., might have errors):

class Main$1<T>
    extends ArrayList<T>
{
    private final List<T> list;

    Main$1(final List<T> a)
    {
        list = a;
    }

    @Override
    public boolean add(T element)
    {
        super.add(element);
        return list.add(element);
    }
}

and

class Main{
     public static <T> List<T> modifiedList(final List<T> list)
     {
         return new Main$1<T>(list);
     }

     public static void main(String[] args) 
     {
         List<String> originalList=new ArrayList<String>();
         List<String> duplicateList=modifiedList(originalList);
         originalList.add("1");
         originalList.add("2");
         originalList.add("3");
         System.out.println(originalList+" "+duplicateList);
         duplicateList.add("4");
         duplicateList.add("5");
         duplicateList.add("6");
         System.out.println(originalList+" "+duplicateList);
     }
like image 174
TofuBeer Avatar answered Sep 27 '22 22:09

TofuBeer