Java Constructors

Learning Goal: Students will understand what Java constructors are, why they are used, and how constructors help create and initialize objects.

What Is a Constructor?

A constructor is a special method that runs automatically when an object is created.

Constructors are used to set up or initialize an object’s data.

Student s1 = new Student();

When the keyword new is used, Java creates a new object and calls the constructor.

Why Do We Use Constructors?

Constructors help us give objects starting values.

For example, if we create a Student object, we may want to immediately give the student:

Without a constructor, we would have to set each value manually after creating the object.

Constructor Rules

  1. The constructor name must be the same as the class name.
  2. A constructor does not have a return type.
  3. Constructors are usually marked public.
  4. A constructor runs automatically when an object is created.
  5. A class can have more than one constructor.

Example Class Without a Constructor

public class Student {

    private String name;
    private int gradeLevel;
}

This class has two instance variables, but there is no constructor written yet.

Java will automatically provide a default constructor if we do not write one.

Default Constructor

A default constructor is a constructor with no parameters.

public class Student {
    private String name;
    private int gradeLevel;

    public Student() {
        name = "Unknown";
        gradeLevel = 9;
    }
}

This constructor gives every new Student object the same starting values.

Student s1 = new Student();

After this line runs, the object stores:

name = "Unknown"
gradeLevel = 9

Constructor With Parameters

A constructor can have parameters so we can give each object different starting values.

public class Student {
    private String name;
    private int gradeLevel;

    public Student(String studentName, int studentGradeLevel) {
        name = studentName;
        gradeLevel = studentGradeLevel;
    }
}

Now we can create different students:

Student s1 = new Student("Maria", 10);
Student s2 = new Student("David", 11);

The first object stores:

name = "Maria"
gradeLevel = 10

The second object stores:

name = "David"
gradeLevel = 11

Constructor Parameters vs. Instance Variables

Instance variables belong to the object.

Parameters are temporary values passed into the constructor.

public class Dog {
    private String name;
    private int age;

    public Dog(String dogName, int dogAge) {
        name = dogName;
        age = dogAge;
    }
}

In this example:

The parameter values are copied into the instance variables.

Using this

Sometimes the parameter names are the same as the instance variable names.

public class Dog {
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

The keyword this means “this object.”

this.name = name;

This means the object’s name variable gets the value from the name parameter.

Why Use this?

Use this when the instance variable and parameter have the same name.

this.age = age;

The left side is the object’s instance variable.

The right side is the parameter.

Multiple Constructors

A class can have more than one constructor. This is called constructor overloading.

public class GamePlayer {
    private String username;
    private int score;

    public GamePlayer() {
        username = "Guest";
        score = 0;
    }

    public GamePlayer(String username) {
        this.username = username;
        score = 0;
    }

    public GamePlayer(String username, int score) {
        this.username = username;
        this.score = score;
    }
}

Now we can create objects in different ways:

GamePlayer p1 = new GamePlayer();
GamePlayer p2 = new GamePlayer("PlayerOne");
GamePlayer p3 = new GamePlayer("PlayerTwo", 500);

Constructor Overloading

Constructor overloading means having multiple constructors with different parameter lists.

The constructors must have different:

public Student() {
}

public Student(String name) {
}

public Student(String name, int gradeLevel) {
}

These are allowed because the parameter lists are different.

Constructor vs. Method

Constructor Method
Has the same name as the class Can have any valid name
Has no return type Usually has a return type
Runs automatically when object is created Runs only when called
Used to initialize objects Used to perform actions

Example Constructor

public Student(String name) {
    this.name = name;
}

Example Method

public void printName() {
    System.out.println(name);
}

Complete Example

public class Car {
    private String make;
    private String model;
    private int year;

    public Car() {
        make = "Unknown";
        model = "Unknown";
        year = 2000;
    }

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

    public void printCarInfo() {
        System.out.println(year + " " + make + " " + model);
    }
}

Using the Class

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car();
        Car car2 = new Car("Toyota", "Camry", 2024);

        car1.printCarInfo();
        car2.printCarInfo();
    }
}

Output

2000 Unknown Unknown
2024 Toyota Camry

Important Vocabulary

Class

A blueprint for creating objects.

public class Student

Object

An actual item created from a class.

Student s1 = new Student();

Instance Variable

A variable that belongs to an object.

private String name;

Constructor

A special method that creates and initializes an object.

public Student() {
}

Parameter

A value passed into a constructor or method.

public Student(String name)

Common Mistakes

Mistake 1: Giving the Constructor a Return Type

Incorrect:

public void Student() {
}

This is not a constructor because it has void.

Correct:

public Student() {
}

Mistake 2: Constructor Name Does Not Match the Class Name

Incorrect:

public class Student {
    public Person() {
    }
}

Correct:

public class Student {
    public Student() {
    }
}

Mistake 3: Forgetting to Initialize Instance Variables

Weak Example:

public Student(String name) {
}

Better Example:

public Student(String name) {
    this.name = name;
}

AP Computer Science A Connection

Constructors are important in AP Computer Science A because they often appear in questions about classes and objects.

You should be able to:

Practice Questions

Practice Question 1

Question: What is the purpose of a constructor?

Answer: A constructor initializes a new object when it is created.

Student s1 = new Student("Ana", 10);

This creates a new Student object and gives it starting values.

Practice Question 2

Question: What is wrong with this constructor?

public void Dog(String name) {
    this.name = name;
}

Answer: The problem is that it has a return type: void.

Constructors do not have return types.

Correct Version:

public Dog(String name) {
    this.name = name;
}

Practice Question 3

Question: What does this.name = name; mean?

Answer: It means the object’s instance variable name gets the value from the constructor parameter name.

this.name = name;

Left side: this.name means the object’s instance variable.

Right side: name means the parameter.

Summary

A constructor is a special method that helps create and initialize an object.

Remember: