Program to count number of object created in java

Sometime in the interview, you may be asked to write a program to count a number of objects created in the application. Let’s see a java program which illustrates how many numbers of objects has been created. Since instance block and constructor will invoke at the time of object creation, we can use further this logic to write a program.

First method- Using instance block.

Output is – number of object created is 7


Second method –

Output is – number of object created is 7