Illustration:
Book[] b = new Book[array_length];
Way 1
ClassName object[]=new ClassName[array_length];
Way 2
ClassName[] objArray;
Way 3
ClassName objectArray[];
Implementation: Array of objects
Employee_department1[20]; Employee_department2[20]; Employee_department3[20];
Example:
// 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 = nzew 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 Gamig"); 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(); } }
Output
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 Gamig Product Object 5: Product Id = 43590 Product Name = Acer Predator Gaming
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.