Write a Java program to create an anonymous object


The code creates a class named Anonymous with a main method. When the main method is executed, an instance of an anonymous inner class Demo is created using the new keyword.

The anonymous class has a constructor that simply prints the message "Constructor" to the console. Therefore, when the anonymous inner class instance is created, the constructor is called and the message "Constructor" is printed to the console.

Source Code

public class Anonymous
{
	public static void main(String[] args)
	{
		new Demo();
	}
}
class Demo
{
	Demo()
	{
		System.out.println("Constructor");
	}
}

Output

Constructor