On the subject of Anonymous classes, the Oracle documentation states that...
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
Now, given that local classes are (to my knowledge) classes defined within a method (or some other local construct) like the following...(where 'MyInterface' is an interface with an abstract 'test' method)
public void localTest(){
MyInterface mi = new MyInterface(){
@Override
public void test(){System.out.println("test");};
};
}
The above is OK and falls within the definition above, however, I can also define the following...
class MyClass{
MyInterface mi = new MyInterface(){
@Override
public void test(){System.out.println("test");};
};
}
This isn't within a method so isn't a 'Local' class and therefore doesn't fall within the above definition. Is there anywhere I can read about these types of anonymous classes (anonymous member classes if you will). What exactly are they if not anonymous classes as defined?
Local classes are defined here as being defined in a block, rather than in a method. Your example is still an anonymous class. If you're in the process of learning, a note here is that you can actually replace the declaration with a lambda expression like so:
Also, anonymous classes are only described as being like local classes, meaning that the former is not necessarily a subset of the latter.