I’m finding this kind of overriding after a new in java code very often
classB body
....
ClassA a = new ClassA(){
@Override
public void funcion(){
atributeClassB = whatever
}
} ;
....
How is it called this kind of constructing ? when is executed the code between brackets ? how this code can have access to a classB attribute ?
in fact if i only know how this way of working is called i can document myselve in google but without a key name i couldn’t find it.
the code where i’ve found it is this here on line 151
It’s called an anonymous local derived class (or “anonymous inner class”, though there’s a difference between just being “inner” and being local [all local classes are inner; not all inner classes are local; more below]). The code within the curly braces forms part of the class definition of the anonymous class.
This
…is effectively equivalent to this:
…where
SubClassAis defined within the containing class:…although there’s a bit more to it than that because the anonymous class in your example is defined within a method (that’s the “local” vs. “inner” thing), more in the various sections starting here.