Sub topic 12
Non-denotable Types and Anonymous Classes
Assume you know Java local variables, reference types, interfaces, and basic anonymous classes. This chapter uses Java 10 or later, because local variable type inference with var was added in Java 10.
What is a non-denotable type?
A denotable type is a type that you can write directly in Java source code, such as String, List<String>, or Customer.
A non-denotable type is a type that exists in the compiler's type system but has no useful name that you can write in a declaration. An anonymous class is one common source of such a type.
An anonymous class is a class created without giving the class a declared name:
var point = new Object() {
int x = 3;
int y = 4;
int distanceSquared() {
return x * x + y * y;
}
};The expression creates an object whose class extends Object. The compiler creates an internal class for it, but you do not write that class's name. The type of point is therefore the anonymous class type.
Because var infers the type from the initializer, the compiler can preserve that type:
System.out.println(point.x); // 3
System.out.println(point.distanceSquared()); // 25If you declared the variable as Object, these members would not be available through the variable:
Object value = new Object() {
int x = 3;
};
// value.x; // Does not compile
// value.distanceSquared(); // Does not compileThe object still has those members at runtime. The problem is that the static type of value is Object, and Object does not declare them.
How `var` preserves the anonymous type
The compiler treats the following declaration as having the anonymous class's inferred type:
flowchart TD
A["new Object() with extra members"] --> B["var infers the anonymous class type"]
B --> C["point.x and point.distanceSquared() are available"]
D["Object declaration"] --> E["Only Object members are available"]The inferred type is fixed at compile time. It does not become a different type later, and Java does not perform dynamic typing.
public class AnonymousDetails {
public static void main(String[] args) {
var point = new Object() {
int x = 3;
int y = 4;
int distanceSquared() {
return x * x + y * y;
}
};
System.out.println(point.x);
System.out.println(point.distanceSquared());
Object asObject = point;
System.out.println(asObject.toString());
}
}The variable point can use the anonymous class's fields and method. The variable asObject refers to the same object, but its static type is only Object.
What you can and cannot do
You can use an inferred anonymous type locally. You cannot use its unnamed type as a method return type, field type, or parameter type.
| Declaration | Result |
|---|---|
| `var item = new Object() { ... };` | Allowed for a local variable |
| `Object item = new Object() { ... };` | Allowed, but anonymous members are hidden |
| `public var makeItem()` | Not allowed |
| `private var item;` | Not allowed |
var itself is restricted to local variable declarations, loop variables, resource variables, and lambda parameters. It does not provide a way to expose an anonymous class through a class API.
If other code must use the object, prefer a named class or an interface. Anonymous classes are most useful when the object has a small, local purpose.
Choosing between `var` and an explicit type
Use var when the extra members of the anonymous class are useful only inside one method and the initializer clearly explains the object.
Use an explicit interface or superclass when the object crosses a method boundary:
interface Formatter {
String format(String text);
}
public class FormatterExample {
public static void main(String[] args) {
Formatter formatter = new Formatter() {
@Override
public String format(String text) {
return text.trim().toUpperCase();
}
};
System.out.println(formatter.format(" hello "));
}
}Here, the interface communicates the intended contract better than exposing an anonymous implementation detail.
Exercise
Why does var point = new Object() { int x = 1; }; allow point.x, while Object point = new Object() { int x = 1; }; does not?
Show answer
var preserves the anonymous class's inferred static type, which declares x. The explicit Object declaration hides that member because Object does not declare x.