How to Create Array of Objects in Java?
Last Updated :
04 Jan, 2025
In Java, an array of objects is used to store multiple instances of a class within a single array. This allows us to easily manage a collection of objects when working with large datasets or collections.
Example:
In the below example, we will demonstrate how to create an array of Student
objects and initialize them with different values. Then, we will display the details of each student object stored in the array.
Java
class Student {
int id;
String n;
// Constructor to initialize student object
Student(int id, String n) {
this.id = id;
this.n = n;
}
// Method to display student details
void display() {
System.out.println("ID: " + id + ", Name: " + n);
}
}
public class Main {
public static void main(String[] args) {
// Creating and initializing an
// array of Student objects
Student[] s = {
new Student(1, "Ram"),
new Student(2, "Shyam")
};
// Displaying student details
for (Student s1 : s) {
s1.display();
}
}
}
OutputID: 1, Name: Ram
ID: 2, Name: Shyam
Explanation: The for
loop iterates over each Student
object in the "s"
array. Inside the loop, s1.display()
is called to print the details of each Student
object.
Creating an Array Of Objects In Java
In Java, we can create an array of objects just like any other array. The only difference is that the array elements are references to objects rather than primitive types.
1. Declaration: To declare an array of objects, specify the class name followed by square brackets []
.
Class_Name[ ] objectArrayReference;
Student[] students;
Alternatively, we can also declare an Array of Objects as,
Class_Name objectArrayReference[ ];
Student students[];
2. Instantiation: After declaring the array, instantiate it using the new
keyword, specifying the size of the array.
Class_Name obj[ ]= new Class_Name[Array_Length];
students = new Student[3]; // An array of 3 Student objects
3. Initialization: Each element of the array must be initialized individually, either via constructor or setter methods.
Structure of an Array Objects
The below figure shows the structure of an Array of Objects (here object is "bike").

Initializing Array Of Objects
After creating an array of objects, the elements need to be initialized. There are two common ways to do this:
1. Using the constructor
At the time of creating actual objects, we can assign initial values to each of the objects by passing values to the constructor separately. Individual actual objects are created with their distinct values.
Example:
Java
// Java program to demonstrate initializing
// an array of objects using constructor
class GFG {
public static void main(String args[]) {
// Declaring an array of student
Student[] arr;
// Allocating memory for 2 objects
// of type student
arr = new Student[2];
// Initializing the first element
// of the array
arr[0] = new Student(1, "Sai");
// Initializing the second element
// of the array
arr[1] = new Student(2, "Omm");
// Displaying the student data
System.out.println(
"Student data in student arr 0: ");
arr[0].display();
System.out.println(
"Student data in student arr 1: ");
arr[1].display();
}
}
// Creating a student class with
// id and name as a attributes
class Student {
public int id;
public String n;
// Student class constructor
Student(int id, String n)
{
this.id = id;
this.n = n;
}
// display() method to display
// the student data
public void display()
{
System.out.println("Student id is: " + id + " "
+ "and Student name is: "
+ n);
System.out.println();
}
}
OutputStudent data in student arr 0:
Student id is: 1 and Student name is: Sai
Student data in student arr 1:
Student id is: 2 and Student name is: Omm
2. Using Setter Methods
We can create objects first and then assign values using setter methods. A member function of the respective class is created and that is used to assign the initial values to the objects.
Example:
Java
// Java program to demonstrate initializing
// an array of objects using a method
class GFG {
public static void main(String args[]) {
// Declaring an array of student
Student[] arr;
// Allocating memory for 2 objects
// of type student
arr = new Student[2];
// Creating actual student objects
arr[0] = new Student();
arr[1] = new Student();
// Assigning data to student objects
arr[0].setData(1, "Sai");
arr[1].setData(2, "Omm");
// Displaying the student data
System.out.println(
"Student data in student arr 0: ");
arr[0].display();
System.out.println(
"Student data in student arr 1: ");
arr[1].display();
}
}
// Creating a Student class with
// id and name as a attributes
class Student {
public int id;
public String n;
// Method to set the data to
// student objects
public void setData(int id, String n)
{
this.id = id;
this.n = n;
}
// display() method to display
// the student data
public void display()
{
System.out.println("Student id is: " + id + " "
+ "and Student name is: "
+ n);
System.out.println();
}
}
OutputStudent data in student arr 0:
Student id is: 1 and Student name is: Sai
Student data in student arr 1:
Student id is: 2 and Student name is: Omm
Similar Reads
How to Calculate Size of Object in Java?
In Java, an object is an instance of a class that encapsulates data and behavior. Calculating the size of an object can be essential for memory management and optimization. This process involves understanding the memory layout of the object, including its fields and the overhead introduced by the JV
2 min read
How to Convert Vector to Array in Java?
As we all know an array is a group of liked-typed variables that are referred to by a common name while on the other hand vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implement the List interface which gives a superior
3 min read
How to convert LinkedList to Array in Java?
Given a Linked List in Java, the task is to convert this LinkedList to Array. Examples: Input: LinkedList: ['G', 'e', 'e', 'k', 's'] Output: Array: ['G', 'e', 'e', 'k', 's'] Input: LinkedList: [1, 2, 3, 4, 5] Output: Array: [1, 2, 3, 4, 5] Approach: Get the LinkedListConvert the LinkedList to Object
2 min read
How to Declare an Array in Java?
In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif
3 min read
How are Java Objects Stored in Memory?
In Java, all objects are dynamically stored in the Heap memory while references to those objects are stored in the stack. Objects are created with the help of "new" keyword and are allocated in the heap memory. However, declaring a variable of a class type does not create an object it only creates r
5 min read
How to Return an Array in Java?
An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of
5 min read
How to Convert HashMap to ArrayList in Java?
In Java a HashMap is a collection that stores key-value pairs on the other hand, an ArrayList is a collection that stores dynamic arrays. There are some scenarios where we need to convert a HashMap into an ArrayList such as:Extracting only the keys or values in the list form.Converting key-value pai
2 min read
Convert List to Array in Java
The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give
5 min read
How to Add an Element to an Array in Java?
In Java, arrays are of fixed size, and we can not change the size of an array dynamically. We have given an array of size n, and our task is to add an element x into the array. In this article, we will discuss the NewDifferent Ways to Add an Element to an ArrayThere are two different approaches we c
3 min read
Set to Array in Java
Given a set (HashSet or TreeSet) of strings in Java, convert it into an array of strings. Input : Set hash_Set = new HashSet(); hash_Set.add("Geeks"); hash_Set.add("For"); Output : String arr[] = {"Geeks", "for"} Method 1 (Simple) We simply create an empty array. We traverse the given set and one by
3 min read