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.
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.
public.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.
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
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
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:
name and age are instance variables.dogName and dogAge are parameters.The parameter values are copied into the instance variables.
thisSometimes 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.
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.
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 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 | 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 |
public Student(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
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);
}
}
public class Main {
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car("Toyota", "Camry", 2024);
car1.printCarInfo();
car2.printCarInfo();
}
}
2000 Unknown Unknown
2024 Toyota Camry
A blueprint for creating objects.
public class Student
An actual item created from a class.
Student s1 = new Student();
A variable that belongs to an object.
private String name;
A special method that creates and initializes an object.
public Student() {
}
A value passed into a constructor or method.
public Student(String name)
Incorrect:
public void Student() {
}
This is not a constructor because it has void.
Correct:
public Student() {
}
Incorrect:
public class Student {
public Person() {
}
}
Correct:
public class Student {
public Student() {
}
}
Weak Example:
public Student(String name) {
}
Better Example:
public Student(String name) {
this.name = name;
}
Constructors are important in AP Computer Science A because they often appear in questions about classes and objects.
You should be able to:
thisQuestion: 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.
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;
}
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.
A constructor is a special method that helps create and initialize an object.
Remember:
new.this refers to the current object.