Tino APCS

Constructors

A constructor is a method with the same name as the class. Constructors are used during object creation to initialize the starting state of an object.

So far you have used constructors to create objects of type SketchPad, DrawingTool, and Color. For example, in order to create a SketchPad object you would use a SketchPad constructor:

Although you have used constructors, you have not written blueprint code for them yet - which is what we're about to do. The format of a constructor is:

public class ClassName {

    // Attributes

    // Constructors public ClassName(param1, param2, param3, …) {
        Use the constructor parameters to initialize some of the attributes
        Initialize the remaining attributes by assigning default values.
    }

    // Methods
}


The parameter list of a constructor is usually a list of attributes you want users of your class to pass in when creating objects of the given class. Start with a short list of essential parameters. For example,

public class Window {

    // Attributes
    int xPos;
    int yPos;
    int width;
    int height;
    Color frameColor;
    Color interiorColor;

    // Constructors public Window(int x, int y) {
        // Use the constructor parameters x and y to initialize the xPos and yPos attributes
        xPos = x;
        yPos = y;

        // Choose default values for the rest of the attributes
        width = 100;
        height = 100;
        frameColor = Color.GRAY;
        interiorColor = new Color(255, 255, 0);
    }

    // Methods
}


You can have as many constructors as you want as long as they have a different set of parameter data types. The constructor shown above had parameters if type (int, int). Therefore, you wouldn't be allowed to have another constructor that has exactly two int parameters, but you could have one that has two ints and a Color as shown below.

public class Window {

    // Attributes
    int xPos;
    int yPos;
    int width;
    int height;
    Color frameColor;
    Color interiorColor;

    // Constructors
    public Window(int x, int y) {
        // Use the constructor parameters x and y to initialize the xPos and yPos attributes
        xPos = x;
        yPos = y;

        // Choose default values for the remaining attributes
        width = 100;
        height = 100;
        frameColor = Color.GRAY;
        interiorColor = new Color(255, 255, 0);
    }
     public Window(int x, int y, Color fColor) {

        // Use the constructor parameters to initialize user-chosen attributes
        xPos = x;
        yPos = y;
        frameColor = fColor;

        // Choose default values for the remaining attributes
        width = 100
        height = 100
        interiorColor = new Color(255, 255, 0);
    }

    // Methods
}


At some point, it's not convenient to have a long list of parameters for every possible attribute in your class. For example,

public Window(int x, int y, int, int w, int h, Color fColor, Color iColor) {
        ...
}


Instead of long constructor parameter lists, you should add helper methods to let users of your class change the values of each kind of attribute. This is discussed in the next pages.

Last modified: December 12, 2022

Back to Attributes

Dark Mode

Outline