DEV Community

Bola Adebesin
Bola Adebesin

Posted on

Constructors in Java

Recently, I've been learning Java. The goal of this article and subsequent writings is to write a short blurb about the topics, I cover each day to help reinforce my learning and hold myself accountable.

Constructors are special methods (functions associated with classes) that create a kind of skeleton that each new object (instance of a class) must match.

I like to think of constructors like a checkpoint when you make a new object. Your object cannot move passed the checkpoint (i.e. your program will throw an error) if the criteria specified by the constructor are not met. And by setting these criteria, we ensure that our objects always have a valid state. When we use constructors all of our variables are initialized.

Example without Constructor:

class Main {
  public static class Car{
    String model; 
    int maxSpeed;

  }


  public static void main(String[] args) {

    Car newCar = new Car(); 
    newCar.model = "Toyota"; 
    newCar.maxSpeed = 100; 

    System.out.println(newCar.model); 
    System.out.println(newCar.maxSpeed);
    }
  }
Enter fullscreen mode Exit fullscreen mode
Output:

image

Without a constructor, I can create a car object newCar without initializing any variables. Instead, I initialize the model and maxSpeed variables on the following lines. Not only does this take up more space. But if I forget to do this step, my program will have errors later on in life.

In the example below, we create a constructor by using the keyword public create the method Car (the same name as our class, this is important), and then pass the variables we want to initialize when our object is created (in this case the model and the maximum speed of the car). By using the this keyword we are referencing the specific object we are creating. so this references this Car not some other car.

Example with Constructor:

class Main {
  public static class Car{
    String model; 
    int maxSpeed;

    public Car(String model, int maxSpeed){
      this.model = model; 
      this.maxSpeed = maxSpeed; 
    }

  }
  public static void main(String[] args) {

    Car newCar = new Car(); 
    newCar.model = "Toyota"; 
    newCar.maxSpeed = 100; 

    System.out.println(newCar.model); 
    System.out.println(newCar.maxSpeed);


    }
  }

The image below shows that when I include the constructor on lines (6-9) I can no longer create the `newCar` object without initializing the variable. The editor underlines the object to indicate an error. 
Enter fullscreen mode Exit fullscreen mode

image

If the code is run the following error is produced:
image

But when we correctly pass the expected arguments to our constructor:

class Main {
  public static class Car{
    String model; 
    int maxSpeed;

    public Car(String model, int maxSpeed){
      this.model = model; 
      this.maxSpeed = maxSpeed; 
    }

  }


  public static void main(String[] args) {

    Car newCar = new Car("Toyota", 100); 
    // newCar.model = "Toyota"; 
    // newCar.maxSpeed = 100; 

    System.out.println(newCar.model); 
    System.out.println(newCar.maxSpeed);


    }
  }
Enter fullscreen mode Exit fullscreen mode

We get the following output:
image

So again, constructors help us ensure that our objects are valid and have everything they need. A great way to dummy proof our code.

Top comments (0)