Constructors and this
What you will learn
A constructor prepares a new object for use. It has the class name and no return type. Constructor parameters can supply required values, and this identifies the current object. Overloaded constructors provide different ways to initialize the same class.
class Book { private String title; Book(String title) { this.title = title; } String title() { return title; }}public class Main { public static void main(String[] args) { Book book = new Book("Atlas"); System.out.println(book.title()); }}Expected output:
AtlasThe constructor parameter shadows the field; this.title selects the object field.
How it works
A constructor such as Book(String title) is selected by the arguments passed to new Book("Atlas"). this.title = title; assigns the parameter to the field. Without this, title = title; would assign the parameter to itself in this example. A constructor is not an ordinary method: adding void makes a method with the same name, not a constructor. When a normal class declares no constructors, the compiler supplies a default constructor; declaring even one constructor removes that automatic no-argument option. You can explicitly write a no-argument constructor, and delegate to another constructor with this(...). For this Java 21-compatible path, place that delegation first; Java 25 has additional flexible constructor-body rules that will be covered separately. Delegate to a shared constructor to avoid inconsistent defaults. A constructor can reject an invalid argument by throwing IllegalArgumentException. This is an unchecked exception indicating the caller supplied an unacceptable value; the full exception topic comes later. Keep validation simple and ensure callers cannot obtain a normally completed object in an invalid initial state.
class Ticket { private int seats; Ticket() { this(1); } Ticket(int seats) { if (seats <= 0) throw new IllegalArgumentException("Seats must be positive"); this.seats = seats; } int seats() { return seats; }}public class Main { public static void main(String[] args) { System.out.println(new Ticket().seats()); System.out.println(new Ticket(4).seats()); }}Expected output:
14Both construction paths use the same validation and assignment.
Check an edge case
class Tag { private String name = "unset"; Tag(String name) { name = name; } String name() { return name; }}public class Main { public static void main(String[] args) { Tag tag = new Tag("blue"); System.out.println(tag.name()); }}Expected output:
unsetThis deliberately misleading assignment compiles but does not update the field. Correct it with this.name = name.
Common mistakes
Do not write void Book(String title) when you mean a constructor. Also, declaring Book(String title) does not leave an implicit Book() available. Add an explicit no-argument constructor only when a meaningful default exists.
Practice
Implement a Ticket class with private int seats. Ticket() creates one seat by delegating to Ticket(int seats). Reject zero and negative seats with IllegalArgumentException. Provide seats() to read the value.
Save each complete example as Main.java in a fresh directory, compile with javac --release 21 Main.java, then run java Main. Each example is independent. A local JDK is required; the ByteMind Java runner is not verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Constructors and this quiz
Check understanding using Java 21-compatible examples and the stated assumptions.
Which declaration is a constructor of Ticket?
What does this.seats select inside Ticket(int seats)?
If Ticket(int seats) is the only declared constructor, is new Ticket() valid?
What is the purpose of this(1) in Ticket()?
What happens if Ticket(int seats) throws before completing?