Array of objects in java

An array can store primitive or non- primitive data types. As you see we used int(Primitive data type) type of array. In this post, we see how java create array of objects and also see the array of objects in java simple example program.
Now we will discuss the array of objects in java(non-primitive data types). Let’s say you want to store a record of students. Each Student has some information like rollNo, name, className, etc. So can store objects of Student in an Array. That’s why we are calling this Array of objects. An Array of objects each index value can store an object.

For example, we will create a class Student that holds information about a student. We want to hold a number of student records so we will use an array of objects in the 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) { = 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.setName("Ravi kant");
		recordOfStudent[0] = firstStudent;
		// Second student record
		Student secondStudent = new Student();
		recordOfStudent[1] = secondStudent;
		// Third student record
		Student thirdStudent = new Student();
		recordOfStudent[2] = thirdStudent;
		// print  record of all student
	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());

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 you can see in the above example, We want to store records of the number of students. So we created objects of each student and stored in Array. We created an Array of objects

Student[] recordOfStudent = new Student[3];

It can store three objects of Student. You can change the size of the Array according to your requirements. After the creation of an array of objects, we initialized the value of each index.

recordOfStudent[0] = firstStudent;
recordOfStudent[1] = firstStudent;
recordOfStudent[2] = firstStudent;
Array of objects in java

Now, the recordOfStudent contains three objects of Student. We are displaying the record of the students by accessing the Array. You can simply access an array by use of subscripts. If you want to know more about the access to the array you can read it in detail.
For Accessing of one dimensional Array
For Accessing of Two dimensional Array

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.