Open In App
Related Articles

Java Program to Access All Data as Object Array

Improve Article
Save Article
Like Article

Java is an object-oriented programming language. Most of the work is done with the help of objects. We know that an array is a collection of the same data type that dynamically creates objects and can have elements of primitive types.

  • Java allows us to store objects in an array. In Java, the class is also a user-defined data type. An array that contains class-type elements is known as an array of objects.
  • It stores the reference variable of the object.
  • This Array is used to implement on data sets that require performing operations on different data types simultaneously same like structures in c, this Objects array provide us the luxury to calculate
  • For example, if we want to write a program that works with books details typically contain various attributes like book id(integer), name(String), Author name(string),..etc which are of various data types, so we construct an array Object for Book and write the methods that fetch out various details of this book.


Book[] b = new Book[array_length];

Here we created an array with instance b for class Book, now we can simply add attribute values for a book with their data types which can be depicted from the image below

In order to create arrays of objects, the syntax is as follows:

Way 1

ClassName object[]=new ClassName[array_length];

Way 2

ClassName[] objArray; 

Way 3

ClassName objectArray[];  

Implementation: Array of objects 

Suppose, let’s consider we have created a class named Employee. We want to keep records of 20 employees of a company having three departments. In this case, we will not create 20 separate variables. Instead of this, we will create an array of objects:




// Java Program to Implement Array Of Objects
// Class 1
// Helper class
// Product class- product Id and product name as attributes
class Product {
    // Member variables
    // Product ID
    int pro_Id;
    // Product name
    String pro_name;
    // Constructor
    Product(int pid, String n)
        pro_Id = pid;
        pro_name = n;
    // Method of this class
    public void display()
        // Print and display the productID and product name
        System.out.print("Product Id = " + pro_Id + "  "
                         + " Product Name = " + pro_name);
// Class 2
// Main class
public class GFG {
    // Main driver method
    public static void main(String args[])
        // Creating an array of product object, or simply
        // creating array of object of class 1
        Product[] obj = new Product[5];
        // Creating & initializing actual product objects
        // using constructor
        // Custom input arguments
        obj[0] = new Product(23907, "Hp Omen Gaming 15");
        obj[1] = new Product(91240, "Dell G3 Gaming");
        obj[2] = new Product(29823, "Asus TUF Gaming");
        obj[3] = new Product(11908, "Lenovo Legion Gaming");
        obj[4] = new Product(43590, "Acer Predator Gaming");
        // Lastly displaying the product object data
        System.out.println("Product Object 1:");
        System.out.println("Product Object 2:");
        System.out.println("Product Object 3:");
        System.out.println("Product Object 4:");
        System.out.println("Product Object 5:");


Product Object 1:
Product Id = 23907   Product Name = Hp Omen Gaming 15
Product Object 2:
Product Id = 91240   Product Name = Dell G3 Gaming
Product Object 3:
Product Id = 29823   Product Name = Asus TUF Gaming
Product Object 4:
Product Id = 11908   Product Name = Lenovo Legion Gaming
Product Object 5:
Product Id = 43590   Product Name = Acer Predator Gaming

Output Explanation: 

  • In the following program, we have created a class named Product and initialized an array of objects using the constructor.
  • We have created a constructor of the class Product that contains the product ID and product name. In the main function, we have created individual objects of the class Product. After that, we have passed initial values to each of the objects using the constructor.
  • Now, this code displays the Product id and name of various brands of gaming laptops.
  • This size of the object array in this code was set, but it can be increased based on the requirement.

Similarly, we can create a generalized Array object to run the Business Logic.


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 07 Nov, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials