With that syntax, you create an anonymous class, which is perfectly legal.
Internally, anonymous classes are compiled to a class of their own, called EnclosingClass$n
where the enclosing class' name precedes the $
sign. and n
increases for each additional anonymous class. This means that the following class is being created:
class Interface$1 implements check {
public void message() {
System.out.println("Method defined in the interface");
}
}
Then, the code in main
compiles to internally use the newly-defined anonymous class:
check t = new Interface$1();
t.message();