An array can hold primitive or non-primitive data types. As shown earlier, we used an int (a primitive type) array. In this post, we look at how to create an array of objects in Java and walk through a simple example program of an array of objects in Java.
Now we turn to the array of objects in Java (non-primitive data types). Suppose you want to keep a record of students. Each Student has details such as rollNo, name, className, and so on. You can store Student objects in an array, which is why we call it an array of objects. In an array of objects, each index can hold one object.
For example, we will define a Student class that holds a student's information. Because we want to keep several student records, we will use an array of objects in Java.
public class Student
{
int rollNo;
String name;
String className;
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
class StudentRecord
{
public static void main(String[] args)
{
Student[] recordOfStudent = new Student[3];
// First student record
Student firstStudent = new Student();
firstStudent.setRollNo(1);
firstStudent.setName("Ravi kant");
firstStudent.setClassName("MCA");
recordOfStudent[0] = firstStudent;
// Second student record
Student secondStudent = new Student();
secondStudent.setRollNo(2);
secondStudent.setName("Rama");
secondStudent.setClassName("MCA");
recordOfStudent[1] = secondStudent;
// Third student record
Student thirdStudent = new Student();
thirdStudent.setRollNo(3);
thirdStudent.setName("Radhe");
thirdStudent.setClassName("MCA");
recordOfStudent[2] = thirdStudent;
// print record of all student
displayStduentRecord(recordOfStudent);
}
public static void displayStduentRecord(Student[] recordOfStudent)
{
for(int i = 0; i < recordOfStudent.length; i++)
{
Student stduent = recordOfStudent[i];
System.out.println("Student rollNo = "+stduent.getRollNo());
System.out.println("Student Name = "+stduent.getName());
System.out.println("Student class = "+stduent.getClassName());
}
}
}
Output:
Student rollNo = 1
Student Name = Ravi kant
Student class = MCA
Student rollNo = 2
Student Name = Rama
Student class = MCA
Student rollNo = 3
Student Name = Radhe
Student class = MCA
As the example above shows, we need to store records for several students. We therefore created an object for each student and placed them in an array. We created an array of objects:
Student[] recordOfStudent = new Student[3];
This array can hold three Student objects. You can adjust the array size to suit your needs. After creating the array of objects, we initialized the value at each index.
recordOfStudent[0] = firstStudent; recordOfStudent[1] = firstStudent; recordOfStudent[2] = firstStudent;
Now recordOfStudent holds three Student objects. We display the student records by accessing the array. You can access an array simply by using subscripts. If you want to learn more about accessing arrays, you can read the details here.
For Accessing a one-dimensional array
For Accessing a two-dimensional array

