Count Number Of Objects Created In A program

+2 Arnab Banerjee · February 13, 2015
How to make a program, which will count number of objects created in that program.

Post a Reply


Oldest  Newest  Rating
+2 Seamus Mackie · February 13, 2015
I would just have a global variable counter and every time an object is created increment the counter variable by one.
+1 Arnab Banerjee · February 13, 2015
Can you help me with the code ??
+1 Developer John · February 13, 2015
Create the base of the program first, then you can get some help.:)
+1 Arnab Banerjee · February 14, 2015
public class count{

static int counter = 0;
static void c(){

public static void main(String args[]){

count c1 = new count();
count c2 = new count();
System.out.println("The number of object is " + counter);


This is my approach. :P :) 
+1 Jay Deshaun · February 14, 2015
I kind of look at this as a hard one...
The only way I think of it is if you call the variable itself and increment it, kind of what you are doing in the code above.
+1 Developer John · February 14, 2015
I don't think there is a certain method in Java you could call in order to get the total number of objects. Oh wait, you could make them enums, but for the easier sake of this program, I would just add an increment statement after every object creation statement. That could be much easier.
+1 Jay Deshaun · February 14, 2015
Yea, it would be,
enums are too confusing for me at this point, and this program looks like it's going to be a simple one so no need for any advance methods :P .
+1 Developer John · February 14, 2015
Well enums aren't too confusing. Take this as an example. Would if I had a program where I needed to get a majority of my objects, but I never used a statement that would add the objects to a specific array(the array I want to invoke so that I can get the objects) before the object's creation statement. Well, enums are a set of predefined constants. The problem I find with this program is how those objects could be constants. I guess it all depends on what they do and the functionality of the program.8-)
+1 Arnab Banerjee · February 14, 2015
I am facing a problem with another code, guys please check that out. here is the link.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator