In the Eclipse CDT plugin, I found this unusual way of initializing a field of an abstract class.
The field ALL refers is a class of the class itself.
abstract public class IndexFilter {
public static final IndexFilter ALL = new IndexFilter() {};
....
}
What is the role of new IndexFilter() {}; ?
Can you explain this initialization?
IndexFilter() {};creates an “anonymous subclass” ofIndexFilter. Since the braces are empty, the subclass does not override anything in the base class. Since IndexFilter is abstract, it cannot be instantiated directly, hence why a subclass is required.