CREATING AN ARRAY OF OBJECTS
Example:
public class Student
{private int stuId;
private double stuGpa;
Student(int n, double gpa)
{ stuId = n;
stuGpa = gpa;
}
public int getStuId( )
{ return stuId;
}
public double getStuGpa( )
{ return stuGpa;
}
}
public class TestStudent
{ public static void main (String [ ] args)
{ // The statement below reserves enough memory for five Student objects
named stu[0] through stu[4]; However, the
//statement does not actually construct those Student objects; instead, you
must call the seven individual constructors.
Student [ ] stu = new Student[5];
//Construct 5 Student objects
for (int i = 0; i < 5; i++)
{ stu[i] = new Student(10 + i , 0.0);
}
//Display data for the five students
for (int i = 0; i < 5 ; i++ )
{ System.out.println(stu[i].getStuId( ) + " " + stu[i].getStuGpa( ) );
}
}
}