Array of Object in Java

Advertisements

Java Array of Object

An object of class represents a single record in memory, if we want more than one record of class type, we have to create an array of class or object. As we know, an array is a collection of similar type, therefore an array can be a collection of class type.

Syntax for creating an array of object


	class ClassName
	{
		datatype var1;
		datatype var2;
		- - - - - - - - - -
		datatype varN;

		method1();
		method2();
		- - - - - - - - - -
		methodN();
	}

	Class-Name [] object = new Class-Name [size];

Example for creating an array of object


	//Employee.java

    import java.util.Scanner;

    class Employee
    {
        int Id;
        String Name;
        int Age;
        long Salary;

        void GetData()           // Defining GetData()
        {

            Scanner sc = new Scanner(System.in);

            System.out.print("\n\tEnter Employee Id : ");
            Id = Integer.parseInt(sc.nextLine());

            System.out.print("\n\tEnter Employee Name : ");
            Name = sc.nextLine();

            System.out.print("\n\tEnter Employee Age : ");
            Age = Integer.parseInt(sc.nextLine());

            System.out.print("\n\tEnter Employee Salary : ");
            Salary = Integer.parseInt(sc.nextLine());

        }

        void PutData()           // Defining PutData()
        {
            System.out.print("\n\t" + Id + "\t" +Name + "\t" +Age + "\t" +Salary);
        }

        public static void main(String args[])
        {

            Employee[] Emp = new Employee[3];
            int i;

            for(i=0;i<3;i++)
                Emp[i] =  new Employee();   // Allocating memory to each object

            for(i=0;i<3;i++)
            {
                System.out.print("\nEnter details of "+ (i+1) +" Employee\n");
                Emp[i].GetData();
            }

            System.out.print("\nDetails of Employees\n");
            for(i=0;i<3;i++)
                Emp[i].PutData();

        }
    }

    Output :

                Enter details of 1 Employee
                Enter Employee Id : 101
                Enter Employee Name : Suresh
                Enter Employee Age : 29
                Enter Employee Salary : 45000

                Enter details of 2 Employee
                Enter Employee Id : 102
                Enter Employee Name : Mukesh
                Enter Employee Age : 31
                Enter Employee Salary : 51000

                Enter details of 3 Employee
                Enter Employee Id : 103
                Enter Employee Name : Ramesh
                Enter Employee Age : 28
                Enter Employee Salary : 47000

                Details of Employees
                101       Suresh      29      45000
                102       Mukesh      31      51000
                103       Ramesh      28      47000

In the above example, we are getting and displaying the data of 3 employee using array of object. Statement 1 is creating an array of Employee Emp to store the records of 3 employees.

Advertisement