Simple Class and Array of Object Java Example Program
Syntax
<custom_object>[] <array_name> = new <custom_object>[<array_length>];
Simple Class and Array of Object Example Program
public class SimpleClassAndArrayOfObject {
static Employee[] generateArray(){
Employee[] employees = new Employee[3];
Employee employee1 = new Employee("Ramesh", 25);
Employee employee2 = new Employee("Suresh", 21);
Employee employee3 = new Employee("Ganesh", 29);
employees[0] = employee1;
employees[1] = employee2;
employees[2] = employee3;
return employees;
}
static void printArray(Employee[] employees){
for (int i = 0; i < employees.length; i++){
System.out.println(employees[i].name+" : "+employees[i].age);
}
}
public static void main(String[] args) {
Employee[] employees = new Employee[3];
employees = generateArray();
System.out.println("The list of employees in format 'name : age' is");
printArray(employees);
}
}
class Employee{
String name;
int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
}
Sample Output
The list of employees in format 'name : age' is
Ramesh : 25
Suresh : 21
Ganesh : 29
Basic Programs
- My First Example Java Program
- Constructor Example Java Program
- Parametrized Method Example Java Program
- Static Class and Its Usage in Java Example Program
- Static Function or Method and Its Usage in Java Example Program
- Default and Parameterized Constructor Java Example Program
- Constructor Chaining Java Example Program
- Singleton Class Using Private Constructor Java Example Program
- Constructor Overloading Example Java Program
- Simple Class and Array of Object Java Example Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Sum Of Three Numbers Example Java Program
- Create Matrix Example Java Program
- Twin Prime Example Java Program
- Compile Time Polymorphism Example Java Program
- Encapsulation Example Java Program