Java classes and objects, constructors and this, with a step-through example.
In one line
A class bundles values and actions into a template; an object is one copy made from that template with new.
Why you need it
Keeping the title, page count and pages read of three books in loose variables quickly gives you title1, pages1, read1 and so on. Adding a book means edits in several places, and it is easy to add pages to the wrong book.
Put the three values and read() in one Book class and each book becomes a single variable. One more book is one more new Book(...) line.
Everyday picture
Think of a cookie cutter and cookies. There is one cutter (the class), but you can stamp out as many cookies (objects) as you like, each with its own toppings. The constructor is the moment you press the cutter and decorate that cookie.
Java code does not run on this page. Compile your changed code with javac on your own computer.
When to use it
You have many records with the same shape (members, orders, books, posts)
You want values and the code that changes them to live together (add pages read, work out progress)
A noun keeps coming up when you explain the program. That noun is a class candidate.
A class that only carries values can be one line: record Point(int x, int y) {}. Java writes the constructor, accessors, equals and toString for you (Java 16+).
Common mistakes
Leaving out this. in the constructor
When the parameter has the same name as the field, title = title; only assigns the parameter to itself. The field stays null.
Wrong
classBook {String title;Book(String title) { title = title; }}publicclassMain {publicstaticvoidmain(String[] args) {Book b = newBook("Small Town");System.out.println(b.title); }}
What javac sayserror: non-static method read(int) cannot be referenced from a static context
Fixed
publicclassMain {publicstaticvoidmain(String[] args) {Book book = newBook(); book.read(10);System.out.println(book.readPages); }}
When you run it10
Comparing objects with ==
== asks whether both sides are the very same object. Use equals to compare contents. A record gets equals for free (Java 16+).
Wrong
recordPoint(int x, int y) {}publicclassMain {publicstaticvoidmain(String[] args) {Point a = newPoint(1, 2);Point b = newPoint(1, 2);System.out.println(a == b); }}
When you run itfalse
Fixed
recordPoint(int x, int y) {}publicclassMain {publicstaticvoidmain(String[] args) {Point a = newPoint(1, 2);Point b = newPoint(1, 2);System.out.println(a.equals(b)); }}
When you run ittrue
Check yourself
Question 1
After a reads 40 pages, what does printing b.readPages show?
publicclassMain {publicstaticvoidmain(String[] args) {Book a = newBook("A", 100);Book b = newBook("B", 100); a.read(40);System.out.println(b.readPages); }}
Show the answer
Answer: 2. 0a and b are separate objects with their own fields. a.read(40) only changes a, so b keeps its starting value 0.
Question 2
Which sentence about constructors is true?
Show the answer
Answer: 1. It runs when an object is created and sets the first values A constructor has no return type at all, and a class may have several as long as their parameters differ (constructor overloading).
Questions people ask
What is the difference between a class and an object?
A class is a blueprint written once in code. An object is a real bundle of values made with new while the program runs. One class can make any number of objects.
When must I write this?
When a parameter has the same name as a field. this.title means the field, plain title means the parameter. With different names you can leave it out.
What if I write no constructor?
Java adds a default constructor with no parameters. Once you write any constructor yourself, that default one is no longer added.