I want to create an anonymous class using a method that returns an instance of a class
class FirstClass {
public FirstClass() {
System.out.println("First class created");
}
}
class SecondClass {
public SecondClass() {
System.out.println("Second class created");
}
public FirstClass getFirstClass() {
return new FirstClass();
}
}
public class Ex1 {
public FirstClass getFirstClass() {
return new FirstClass();
}
public static void main(String[] args) {
//here is the problem
Object obj = new SecondClass().getFirstClass() {
{
System.out.println("Anonymous class created");
}
};
}
}
I expect program to work by creating the anonymous class, but it gives me a syntax error. Any solutions or workarounds are accepted. Thanks