Skip to main content

Simple Class and Array of Object Example in Java

2 min read Updated May 29, 2026
Share:
On this page (9sections)

Introduction

Simple Class and Array of Object is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

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

When to use

Use this simple class and array of object example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. Employee[] employees = new Employee[3]; updates a variable used in the calculation or output.

  3. Employee employee1 = new Employee("Ramesh", 25); updates a variable used in the calculation or output.

  4. Employee employee2 = new Employee("Suresh", 21); updates a variable used in the calculation or output.

  5. Employee employee3 = new Employee("Ganesh", 29); updates a variable used in the calculation or output.

  6. employees[0] = employee1; updates a variable used in the calculation or output.

  7. employees[1] = employee2; updates a variable used in the calculation or output.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Name classes in PascalCase and follow one public class per file when starting out.
  • Keep main short — delegate work to other methods as programs grow.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Simple Class and Array of Object program demonstrate?
It shows how to implement simple class and array of object in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials