Reputation: 3887
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?
Upvotes: 0
Views: 94
Reputation: 108994
Both examples you show are anonymous classes. A true local class is a class definition in a method (or other code block), with an actual name (so, not anonymous). Given your example, an equivalent local class would be:
public void localTest(){
class LocalClass implements MyInterface {
@Override
public void test(){
System.out.println("test");
}
}
MyInterface mi = new LocalClass();
}
In my opinion you should hardly ever need a local class. I think I have only tried to use it once, to only quickly refactor it when I got a grip on what I actually needed.
The most important difference between local classes and anonymous classes is that you can reuse a local class within the same method (that is create multiple instances in the same method; without resorting to loops or lambdas).
Furthermore, as you actually have class definition, you can also define and call methods that aren't defined in the interface or super-class. Prior to Java 10 and the introduction of var
, this was not possible with anonymous classes.
Other minor differences are that local classes can be abstract or final, and local classes can extend (and be extended by) other local classes, while an anonymous class is not final and cannot be abstract, but anonymous classes cannot be extended by other classes.
For more information regarding the difference between local classes and anonymous classes, see the Java Language Specification, specifically 14.3. Local Class Declarations and 15.9.5. Anonymous Class Declarations and related sections.
Upvotes: 1
Reputation: 1241
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:
MyInterface mi = () -> System.out.println("test");
Also, anonymous classes are only described as being like local classes, meaning that the former is not necessarily a subset of the latter.
Upvotes: 0