# 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.

```package countnumberofobject;

public class CountObjectExample {

static int countObject = 0;

//using instance block since instance block will invoke at object creation

{
countObject++;
}
public static void main(String[] args) {
CountObjectExample object1 = new CountObjectExample();
CountObjectExample object2 = new CountObjectExample();
CountObjectExample object3 = new CountObjectExample();
CountObjectExample object4 = new CountObjectExample();
CountObjectExample object5 = new CountObjectExample();
CountObjectExample object6 = new CountObjectExample();
CountObjectExample object7 = new CountObjectExample();

System.out.println("number of object created is "+countObject);
}
}
```

Output is – number of object created is 7

Second method –

```package countnumberofobject;

public class CountObjectExample2 {

static int countObject = 0;

//constructor will invoke at object creation
CountObjectExample2(){
countObject++;
}

public static void main(String[] args) {
CountObjectExample2 object1 = new CountObjectExample2();
CountObjectExample2 object2 = new CountObjectExample2();
CountObjectExample2 object3 = new CountObjectExample2();
CountObjectExample2 object4 = new CountObjectExample2();
CountObjectExample2 object5 = new CountObjectExample2();
CountObjectExample2 object6 = new CountObjectExample2();
CountObjectExample2 object7 = new CountObjectExample2();

System.out.println("number of object created is "+countObject);
}
}
```

Output is – number of object created is 7
{"ft_lang_label":"__label__en","ft_lang_prob":0.59639174,"math_prob":0.61524165,"size":1839,"snap":"2021-31-2021-39","text_gpt3_token_len":389,"char_repetition_ratio":0.36730245,"word_repetition_ratio":0.17777778,"special_character_ratio":0.20826536,"punctuation_ratio":0.13333334,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9789709,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-08-02T03:31:41Z\",\"WARC-Record-ID\":\"<urn:uuid:e45a83d3-e92e-4606-b667-844b989db1d6>\",\"Content-Length\":\"132763\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7fa5a324-2665-4249-b0f9-6a787e7314fc>\",\"WARC-Concurrent-To\":\"<urn:uuid:32ec3244-6f22-4d0e-b813-a2e4fbfe9bd4>\",\"WARC-IP-Address\":\"104.21.31.16\",\"WARC-Target-URI\":\"https://www.netsurfingzone.com/core-java/program-to-count-number-of-object-created-in-java/\",\"WARC-Payload-Digest\":\"sha1:NCRQHT3ARWRB34ZKFSI3B637LNIZSAXU\",\"WARC-Block-Digest\":\"sha1:OSGUYJLHCRE3JJKRZJU3KHKRQXC6VFVE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046154302.46_warc_CC-MAIN-20210802012641-20210802042641-00104.warc.gz\"}"}