Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing anonymous class

Tags:

java

I am passing an interface as anonymous implementation to a different object like this:

public interface Interface {
    public int convert (int a);
}



public static void main(String[] args) throws IOException, InterruptedException {

    final int[] array = {1,6,3,5,7,8,4,0,3};
    Interface inter = new Interface() {         
        public int convert(int a) {
            int result = a;             
            for (int i = 0; i < array.length; i++) {
                a=a+array[i];
            }               
            return a;
        }
    };

    SomeObject ty = new SomeObject ();
    ty.Test(7, inter);
}

public class SomeObject {   

    public void Test(int number, Interface inter) {             
        System.out.println(inter.convert(number));
    }
}

My question is: how does it work? How does SomeObject know about the array which is not passed directly to the object (array is not a member of the anonymous class).

Update
(sorry for late update)

what about member vars or methods methods that are used in the anonymous class? they are not final

Interface inter = new Interface() {         
    public int convert(int a) {
        int result = a + someMemberVar;             
        for (int i = 0; i < array.length; i++) {
            a=a+array[i];
        }               
        return a;
    }
};
like image 379
omrid Avatar asked Dec 20 '11 15:12

omrid


People also ask

Can anonymous class be passed as a parameter?

Yes, by adding an initializer method that returns 'this', and immediately calling that method: int myVariable = 1; myButton. addActionListener(new ActionListener() { private int anonVar; public void actionPerformed(ActionEvent e) { // How would one access myVariable here? // It's now here: System.

What's meant by anonymous class?

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.

Does Anonymous class have name?

A nested class that doesn't have any name is known as an anonymous class. Anonymous classes usually extend subclasses or implement interfaces. The above code creates an object, object1 , of an anonymous class at runtime. Note: Anonymous classes are defined inside an expression.

Can Anonymous classes extend other classes?

Anonymous classes are inner classes with no name.We may either extend an existing class or implement an interface.


2 Answers

My question how does it work? How SomeObject knows about the array which is not passed directly to the object (array is not a member of the anonymous class).

  • Answer: It doesn't; unless you declare array as final.
  • Reasoning: It works (if array is final) because since it's final, it can be treated like a constant, since the anonymous function knows that the array cannot be changed.

what about member vars or methods methods that are used in the anonymous class? they are not final

  • Answer: Instance variables and methods (as well as static variables and methods) are in scope anyway, when the anonymous class is created.
like image 168
Jon Newmuis Avatar answered Oct 19 '22 21:10

Jon Newmuis


Since, the method-local variable is declared directly inside a method block it is visible only within the method, starting from the next statement after it's declaration. And by method-local I mean the variable that is declared inside the method but outside any other blocks (e.g. while { /* not here */ }) that might appear within that method.

And if you see, your anonymous class is also defined as a local class within the same method after the array's declaration. That's why it is visible to the class.

The reason that you need to make array final to be able to use it inside the anonymous class is that local variables live only till the method lives but the object of the local class might live longer (even after the method is done).

like image 33
Bhesh Gurung Avatar answered Oct 19 '22 20:10

Bhesh Gurung