Object Array using Java Programming Language.

In this tutorial you will learn How to create Object Array using Java Programming Language. The below example is tested and output is attached in the same article.

Customer.java

Here we are creating Customer class with attributes such as Customer ID and Customer Name. We are accepting the attributes the time of object creation through the constructor arguments. the getCustomerInformation() method is going return the String value such as Customer ID and Customer Name with appended string data.

package com.dineshkrish;
public class Customer {
private int customerId;
private String customerName;
public Customer(int customerId, String customerName) {
this.customerId = customerId;
this.customerName = customerName;
}
public String getCustomerInformation() {
return "Customer Id :"+this.customerId+" Customer Name : "+this.customerName;
}
}

ObjectArray.java

Inside the main() method of ObjectArray class. We are defining the Array Object for Customer type. with size of 3. then assigning the elements in the array object(Customer type) for each position starts from 0 to n-1. by using the for loop iterating and printing the results in presentable manner.

package com.dineshkrish;
public class ObjectArray {
public static void main(String[] args) {
// Defining the Customer Array Object
Customer[] customers = new Customer[3];
customers[0] = new Customer(1001, "John"); // Instance 1
customers[1] = new Customer(1002, "Smith"); // Instance 2
customers[2] = new Customer(1003, "William"); // Instance 3
System.out.println("-------------Customer List----------------");
for (int i = 0; i < customers.length; i++) {
System.out.println(customers[i].getCustomerInformation());
}
System.out.println("-------------------------------------------");
}
}

Output

————-Customer List—————-
Customer Id :1001 Customer Name : John
Customer Id :1002 Customer Name : Smith
Customer Id :1003 Customer Name : William
——————————————-

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *