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
        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 name;
  
    // Student class constructor
    Student(int id, String name)
    {
        this.id = id;
        this.name = name;
    }
  
    // display() method to display
    // the student data
    public void display()
    {
        System.out.println(“Student id is: ” + id + ” ”
                           + “and Student name is: ”
                           + name);
        System.out.println();
    }
}

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

More questions on [categories-list]

0
inline scripts encapsulated in