Here I have attached the Simple Class Array Example in Java Program.

package com.javatraineronline;
public class Customer {
// Attributes
private int customerId;
private String customerName;
private int customerAge;
// Setter and Getter
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getCustomerAge() {
return customerAge;
}
public void setCustomerAge(int customerAge) {
this.customerAge = customerAge;
}
}
package com.javatraineronline;
public class ClassArray {
public static void main(String[] args) {
// Declaring the Customer Array Object
Customer[] customerArray = new Customer[3];
// Creating the Customer Objects
Customer c1 = new Customer();
c1.setCustomerId(101);
c1.setCustomerName("Johny");
c1.setCustomerAge(25);
Customer c2 = new Customer();
c2.setCustomerId(102);
c2.setCustomerName("James");
c2.setCustomerAge(28);
Customer c3 = new Customer();
c3.setCustomerId(103);
c3.setCustomerName("Smith");
c3.setCustomerAge(30);
// Adding the object to an array
customerArray[0] = c1;
customerArray[1] = c2;
customerArray[2] = c3;
// Iterating the Customer Array Object
for (int i = 0; i < customerArray.length; i++) {
Customer customer = customerArray[i];
// Printing the customer object attributes
System.out.println("Customer Id : "+customer.getCustomerId()+"|Customer Name : "+customer.getCustomerName()+"|Customer Age : "+customer.getCustomerAge());
}
}
}

[su_box title=”Output for ClassArray.java”]Customer Id : 101|Customer Name : Johny|Customer Age : 25
Customer Id : 102|Customer Name : James|Customer Age : 28
Customer Id : 103|Customer Name : Smith|Customer Age : 30
[/su_box]

Tags:

No responses yet

Leave a Reply

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