How to Count the Number of Objects in a Class in Java 


               


How to Count the Number of Objects in a Class in Java

Java


In this article, we go over how to count the number of objects in a class in Java.

We can keep track of the number of objects that have been created in a class using a static variable.

Because a static variable is linked to a class and not to an object, it is perfect to create a static variable to keep track of the number of objects created.

We initially set this variable to 0.

Then in the constructor of the class, we can put this variable with an increment operator so that it increases by 1 in the constructor.

Because a constructor is called every time an object is instantiated, this code works. Whenever an object is instantiated, the constructor is called, the object is created, and the static variable keeping the count of the number of objects increases by 1.

So every time we instantiate (create) a new object, we can keep track of this through a static property. Thus, we can count the number of objects instantiated for a given class.

So in the Java code below, we have created a program that counts the number of objects created for a class in Java.



So in this code, we create a class named students.

In this class, we create 3 variables.

The first 2 variables are nonstatic variables, so they are linked to the objects. They are object properties. In this program, we assign a name and an age to each student.

The third variable is a static variable of int type named numberofobjects. We initially set to 0 at the start of the program, because no objects have been instantiated yet. So it begins at a value of 0.

We then create our class constructor.

This constructor takes in 2 parameters, the name and age of the student.

In the body of this constructor, we assign the properties of the object based on the values we pass into the object at instantiation.

In the third line, we take the static variable we created called numberofojbects and increment by 1 in the constructor.

Each time an object is created, the constructor is automatically called and the static variable numberofobjects increases by 1. This is how we are able to keep track of all objects created in the class.

Below we'll create a test class that tests the above program.

In this test class, we create a few objects and then output at the end how many objects have been created and that exist in the class.



So we create a test class named studentsdemo.

We put our main method in this studentsdemo test class.

We then create 4 objects of the Students class, student1, student2, student3, and student4.

With each object that we instantiate, we automatically call the constructor method. This method increments the numberofobjects variable by 1. Being that we have created 4 objects, the numberofobjects variable goes to the value of 4.

We then otuput the number of objects (students) in the class.

Since the numberofobjects variable is a static variable that is not in its original class, we have to reference it by the class name, followed by a dot and the variable name.

Running this test class, we get the following output shown below.

Java Output

There are 4 objects in this class


So this is all that is required to be able to keep track of the number of objects in a class.

Being that a static variable is linked to a class and not an object, it keeps a kind of outside view of objects. So it is able to keep track of objects as a whole being that it's independent of objects. By putting the static variable with an increment operator in the constructor method, we can increment the value by 1 each time the constructor is called (when a new object is created). This allows us to keep track of the number of objects that have been created for a class.

If you have multiple constructors in a class (overloading constructors), it works in exactly the same way. You just put the static variable with an increment of 1 in each constructor. It doesn't matter which constructor is called, if any is called, a new object has been created, so the count increases by 1 regardless of which constructor is called, because in any case a new object has been created.


HTML Comment Box is loading comments...