Java Reference
In-Depth Information
This array is 500 references of type Employee, each initialized to null.
If you want 500 Employee objects, you need 500 new statements. The
following statement uses new only once, and the object that is
instantiated is the array. No Employee objects are instantiated.
new Employee[500]
The elements in myCompany are accessed just like any other array element:
by using an index. The following statement assigns the 228th element to a new
Employee object:
myCompany[227] = new Employee(“George Washington”, “Mount Vernon”, 1);
To invoke a method on this Employee object, you use both the index and the
dot operator. Assuming that the Employee class has a mailCheck() method, the
following statement invokes it on the 228th element in the myCompany array:
myCompany[227].mailCheck();
To demonstrate using an array of references, we will create an array for a
collection of Employee objects, using the following Employee class:
public class Employee
{
public String name;
public String address;
public int number;
public Employee(String name, String address, int number)
{
System.out.println(“Constructing an Employee”);
this.name = name;
this.address = address;
this.number = number;
}
public void mailCheck()
{
System.out.println(“Mailing a check to “ + this.name
+ “ “ + this.address);
}
}
The following ArrayDemo program instantiates and uses several arrays.
Study the program and try to determine its output:
Search WWH ::




Custom Search