Javatpoint Logo
Javatpoint Logo

Array of Class Objects in Java

The Classes and the objects are the foundation of the Java programming language because it is an object-oriented language. We have used a variables of type Object whenever we need to store just one object in our programme. However, using an Array of items is better when dealing with a large number of objects.

A collection of objects, the term alone implies that this stores a variety of the items. An array of objects contains the objects, which means that the objects were held as elements of an array, as opposed to the typical array which holds elements such String, integer, Boolean, etc. Keep in mind that when we refer to a "Array of Objects," what is actually stored inside the array is the reference to the object, not the actual object.

An array is a data structure that stores a fixed-size sequence of the elements of the same type. In Java, arrays are the objects, and they can be used to store the objects of any class type. It means that an array of class objects can be created and used to store instances of a particular class.

Array of Objects

After the object array is created, it needs to be initialised with values. Since the array is not the same as the array of primitive types, we are unable to initialise it in the same manner as we do with primitive types. Each element of the array, or each object or object reference, must be initialised when using an array of objects.

Creating an array of class objects in Java is similar to creating an array of any other type. The syntax for creating an array of class objects is as follows:

For example, to create an array of objects of the class "Person", we would use the following code:

This creates an array named "people" that can hold up to 10 objects of the class "Person". However, it should be noted that the array is initially filled with null values and the objects needs to be initialized before they can be used.

Once an array of class objects is created, we can use the standard array access notation to access and manipulate the elements of the array. For example, to access the first element of the "people" array, we would use the following code:

We can also use loops to iterate through the elements of an array of class objects. For example, the following code will print the names of all the people in the "people" array:

Arrays of class objects can also be used to store objects of different classes that are related through the inheritance. For example, if we have a class "Student" that extends the "Person" class, we could create an array of "Student" objects and store them in an array of "Person" objects:

It is possible because a "Student" object is also a "Person" object, and the "Student" class inherits all the properties and methods of the "Person" class.

Arrays of class objects also allow us to use the functionality of the class they are holding. For example, if we have a class "Car" which has the method "drive()", we can use this method on all the elements of the array of cars.

In conclusion, arrays of class objects in Java are a powerful tool for storing and manipulating collections of objects of a particular class or related classes. They can be created and used in a similar way to arrays of other types, and they allow for easy access and manipulation of the elements of the array. Additionally, the class objects stored in the array can be used to access and use the functionality of the class.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA