Class clazz = new Object(){}.getClass();
Why is this possible and what would it mean? Could someone please remind me?
For example:
public class Testing {
public static void main(String[] args) {
Class clazz = new Object(){}.getClass();
System.out.println(clazz);
}
}
The result is: class Testing$1
new Object(){} creates an anonymous inner class, as a subclass of Object. It is an inner class of Testing, thus it gets a compiler generated name like the one you saw.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With