Multiple constructors

Multiple constructors

A class can have multiple constructors that assign the fields in different ways. Sometimes it’s beneficial to specify every aspect of an object’s data by assigning parameters to the fields, but other times it might be appropriate to define only one or a few.

Spot sp1, sp2;

void setup() {
  size(640, 360);
  // Run the constructor without parameters
  sp1 = new Spot();
  // Run the constructor with three parameters
  sp2 = new Spot(width*0.5, height*0.5, 120);

void draw() {

class Spot {
  float x, y, radius;
  // First version of the Spot constructor;
  // the fields are assigned default values
  Spot() {
    radius = 40;
    x = width*0.25;
    y = height*0.5;
  // Second version of the Spot constructor;
  // the fields are assigned with parameters
  Spot(float xpos, float ypos, float r) {
    x = xpos;
    y = ypos;
    radius = r;
  void display() {
    ellipse(x, y, radius*2, radius*2);

Functions Used


Defines the dimension of the display window width and height in units of pixels

Learn More

Draws an ellipse (oval) to the screen

Learn More

The setup() function is run once, when the program starts

Learn More

Stops Processing from continuously executing the code within draw()

Learn More

Called directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called

Learn More

The background() function sets the color used for the background of the Processing window

Learn More