From the free course AP Computer Science A on Lykke
3.4 Constructors
A constructor is the code that gives a newly created object its initial state. When a program creates a Book, the constructor can ensure that the book begins with a title, an author, and a valid page count instead of existing as an uninitialized bundle of data.
3.4 Constructors
A constructor is the code that gives a newly created object its initial state. When a program creates a Book, the constructor can ensure that the book begins with a title, an author, and a valid page count instead of existing as an uninitialized bundle of data.
Learning Objective 3.4.A: Create objects by calling constructors.
Essential Knowledge 3.4.A.1: Constructors are used to initialize the instance variables of a class.
From class blueprint to initialized object
A class describes what an object should contain; a constructor performs the setup required when that object comes into existence. The new operator creates the object, and the constructor call supplies the initial information.
Consider a library system that creates books from a title and page count:
public class Book {
private String title;
private int pages;
public Book(String bookTitle, int pageCount) {
title = bookTitle;
pages = pageCount;
}
}
The constructor is Book(String bookTitle, int pageCount). Its name is exactly the class name, Book, and it has two parameters: a String followed by an int. Inside the constructor, the assignments initialize the new object’s instance variables.
A client can call that constructor like this:
Book favorite = new Book("The Hobbit", 310);
The execution sequence is:
new Book(...)requests a newBookobject.- Java selects the constructor whose parameter list matches
String, int. "The Hobbit"is assigned tobookTitle.310is assigned topageCount.- The constructor assigns those values to
titleandpages. - The reference
favoritestores the location of the initialized object.
Declaration versus signature
A frequent source of lost points is treating the entire constructor declaration as its signature. These are related but not identical:
| Term | Example | What it includes |
|---|---|---|
| Constructor declaration | public Book(String, int) |
Access modifier, constructor name, and parameter types |
| Constructor signature | Book(String, int) |
Constructor name and parameter types |
| Constructor call | new Book("The Hobbit", 310) |
Class name followed by argument values |
Essential Knowledge 3.4.A.2: A constructor signature consists of the constructor name and its parameter types. In
public Book(String, int), the signature isBook(String, int);publicis an access modifier in the declaration, not part of the signature.
The parameter types matter for identifying the constructor, not the parameter names. Therefore, these declarations have the same signature and cannot both appear in one class:
public Book(String bookTitle, int pageCount) { /* ... */ }
// Book(String, int)
public Book(String name, int numberOfPages) { /* ... */ }
// Also Book(String, int)
Rules that make a constructor a constructor
A constructor has the same name as its class and does not declare a return type—not even void. A method named Book with a return type would not be a constructor; it would be an invalid method declaration because Java method names may not normally match the class name in that form.
A constructor may have no parameters or may accept parameters. A no-argument constructor initializes an object using fixed or default values:
public class Counter {
private int count;
public Counter() {
count = 0;
}
}
A parameterized constructor receives values from the object-creation statement, allowing each object to begin differently:
Counter first = new Counter();
Counter second = new Counter();
Both Counter objects begin with count equal to 0, but they are still separate objects. The constructor initializes each object when that object is created; it does not create one shared instance.
Default constructors and explicit constructors
If a class contains no constructor declaration, Java provides a default no-argument constructor. Once the programmer writes any constructor, Java no longer automatically supplies that default constructor.
This change can cause an unexpected error:
public class Book {
private String title;
public Book(String bookTitle) {
title = bookTitle;
}
}
Book b = new Book(); // Error: no matching no-argument constructor
The class has only the signature Book(String). If both forms are needed, the programmer must declare both constructors explicitly.
Misconception check
Misconception: “A constructor returns the new object, so it needs a return type.” The new expression produces the reference to the object. The constructor’s job is to initialize that object, and its declaration has no return type.
Misconception: “Changing parameter names creates a different constructor.” It does not. Book(String title, int pages) and Book(String name, int count) have the same signature because their parameter-type sequence is identical.
AP computational thinking practices
Constructors connect directly to the official computational thinking practices:
- Computational Thinking Practice 1: Design Code — choose which information an object must receive at creation time and design a constructor signature that supports that design.
- Computational Thinking Practice 2: Develop Code — write the constructor declaration and assignments that initialize instance variables.
- Computational Thinking Practice 3: Analyze Code — trace a
newexpression, match its arguments to a constructor signature, and determine the resulting object state. - Computational Thinking Practice 4: Document Code and Computing Systems — describe what a constructor initializes and what arguments a caller must provide.
Retrieval check: What is the signature of private Ticket(String code, int zone)? Why would new Ticket() fail if that is the only constructor declaration?
Answer: The signature is Ticket(String, int). The declaration is private Ticket(String, int), but private is not part of the signature. new Ticket() fails because no no-argument constructor matches the empty parameter list.

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A