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;
}
};
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.
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.
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.
Anonymous classes are inner classes with no name.We may either extend an existing class or implement an interface.
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).
array
as final.what about member vars or methods methods that are used in the anonymous class? they are not final
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With