how to get an object from array of objects in java

The solution for “how to get an object from array of objects in java” can be found here. The following code will assist you in solving the problem.

// 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(1701289270, “Satyabrata”);
        // Initializing the second element
        // of the array
        arr[1] = new Student(1701289219, “Omm Prasad”);
        // Displaying the student data
            “Student data in student arr 0: “);
            “Student data in student arr 1: “);
// Creating a student class with
// id and name as a attributes
class Student {
    public int id;
    public String name;
    // Student class constructor
    Student(int id, String name)
    { = id; = name;
    // display() method to display
    // the student data
    public void display()
        System.out.println(“Student id is: ” + id + ” ”
                           + “and Student name is: ”
                           + name);

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

inline scripts encapsulated in