Objects in Arrays, Methods, and Fields

This section will show how to use objects in combinations with other programming constructs – as elements in an arrya, as parameters to methods, and as fields in other classes.

Arrays of objects

We have already seen how to create arrays of things like ints and doubles, but we can also create arrays of objects, where each element has a class type. The format for declaring an array of objects is just like declaring any other array:

type[] name;

But here, type should be the name of a class. We can also create space for the array just like we’ve done before:

name = new type[size];

Elements in arrays are initialized to the default value of that type, so elements in an array of objects are automatically initialized to null.

To see an example, recall the Rectangle class from earlier in the chapter. Suppose that we want to create an array of 10 rectangles whose values are inputted by the user. Then, we want to print the area and perimeter of each rectangle. Here’s how:

//Declare the array and allocate space
Rectangle[] rectArray = new Rectangle[10];

Scanner s = new Scanner(System.in);

//Get information about each rectangle
for (int i = 0; i < rectArray.length; i++) 
{
    System.out.print("Enter the length: ");
    int length = s.nextInt();
    System.out.print("Enter the width: ");
    int width = s.nextInt();

    //Create a new Rectangle object with the
    //correct dimensions, and store it in the array
    rectArray[i] = new Rectangle(length, width);
}

//Loop to print the area and perimeter of each rectangle
for (int i = 0; i < rectArray.length; i++) 
{
    System.out.printf("Rectangle %d: area %d, perimeter %d%n", i, rectArray[i].area(), rectArray[i].perim());
}

Notice that we’re treating each object in the array exactly like we would any other object, but instead of having to create a separate variable to refer to each object, we can store them all in an array.

Passing objects to methods

We can pass objects to methods just like any other type of element. If we pass an object to a method, the type for that parameter is the type of the object (the name of the class). Suppose we want to write a printRectangle method that takes a Rectangle object as a parameter and then prints its area and perimeter. Here’s how the method would look:

public void printRectangle(Rectangle r) 
{
    //Now we can treat r just like any other Rectangle object
    System.out.printf("Area: %d%n", r.area());
    System.out.printf("Perimeter: %d%n", r.perim());
}

Now, suppose we’re in the same class as the printRectangle method. Then we could do:

Rectangle rect = new Rectangle(3, 4);
printRectangle(rect);

Notice that passing objects to methods works exactly like passing other types to methods – the only difference is that the parameter type is now the name of a class.

Objects as fields

We can also make objects be fields in another class – again, this works just like other types of fields, but the type for the field will be the name of a class. For example, suppose we have the following Person class:

public class Person 
{
    public String name;
    public int age;

    public Person(String n, int a) 
    {
        name = n;
        age = a;
    }
}

Now, suppose we want to write a Child class that holds a child’s name, grade, school, and parent information. Each parent will be stored as a Person field. Here is the Child class:

public class Child 
{
    private String name;
    private int grade;
    private String school;
    private Person mother;
    private Person father;

    //n: name, g: grade, s: school
    //mn: Mom’s name, ma: Mom’s age
    //fn: Dad’s name, fa: Dad’s age
    public Child(String n, int g, String s, String mn,
    int ma, String fn, in fa) 
    {
        name = n;
        grade = g;
        school = s;

        //Initialize fields to be new objects
        //with corresponding names and ages
        mother = new Person(mn, ma);
        father = new Person(fn, fa);
    }
}

Now, suppose we are outside both the Person and Child classes and we want to create a Child object with the following information:

  • Name: Fred
  • Grade: 5th
  • School: Bluemont Elementary
  • Mom: Donna, 34
  • Dad: Frank, 35

Here’s what we would do:

Child c = new Child("Fred", 5, "Bluemont Elementary", "Donna", 34, "Frank", 35);