Java Program to Access All Data as Object Array
Last Updated :
07 Nov, 2022
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.
Illustration:
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:
Employee_department1[20];
Employee_department2[20];
Employee_department3[20];
Example:
Java
// 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);
System.out.println();
}
}
// 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:");
obj[0].display();
System.out.println("Product Object 2:");
obj[1].display();
System.out.println("Product Object 3:");
obj[2].display();
System.out.println("Product Object 4:");
obj[3].display();
System.out.println("Product Object 5:");
obj[4].display();
}
}
OutputProduct 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.
Similar Reads
Java Program to Convert Byte Array to Object Converting byte array into Object and Object into a byte array process is known as deserializing and serializing. The class object which gets serialized/deserialized must implement the interface Serializable. Serializable is a marker interface that comes under package 'java.io.Serializable'.Byte Arr
2 min read
Java Program to Convert an Array into a List In Java, arrays and lists are two commonly used data structures. While arrays have a fixed size and are simple to use, lists are dynamic and provide more flexibility. There are times when you may need to convert an array into a list, for instance, when you want to perform operations like adding or r
4 min read
Java Program to Change a Collection to an Array An array is a data structure that can hold a fixed-size, homogeneous collection of elements of the same data type, which can be either primitive data types (e.g., int, float) or object references. However, the size of the array cannot be changed once it is created. On the other hand, a collection is
3 min read
Java Program to Convert String to Object In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object. Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the me
2 min read
Java Program to Print the Elements of an Array An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It's important to note that arrays in Java function differently than they do in C/C++As you see, the array of size 9 holds elem
6 min read
How to Convert an ArrayList of Objects to a JSON Array in Java? In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList of objects to a JSON array in Java. Steps to Convert an ArrayList of Obje
3 min read
Conversion of JSON Object Array to Java POJO In this article, we will learn how to use the widely used Jackson JSON library to map an array of JSON items to a Java POJO class instance. PrerequisitesA basic understanding of Java programming.A JSON library for Java, such as Jackson, Gson, or org.json, depending on your preference.Implementation
3 min read
Java Array Programs An array is a data structure consisting of a collection of elements (values or variables), of the same memory size, each identified by at least one array index or key. An array is a linear data structure that stores similar elements (i.e. elements of similar data type) that are stored in contiguous
4 min read
How to Convert a Vector of Objects into a JSON Array in Java? In Java, the conversion of data structures like Vector to JSON (JavaScript Object Notation) format is required especially while working with the APIs or data interchange between different systems. The conversion of the Vector of an Object to the JSON Array has various approaches that we will learn i
3 min read
Program to Convert Stream to an Array in Java A Stream is a sequence of objects that support various methods which can be pipelined to produce the desired result. An array is a group of like-typed variables that are referred to by a common name. An array can contain primitives data types as well as objects of a class depending on the definition
3 min read