Introduction: A Cake That Comes Out Complete
Imagine that every time you buy a cake, you get a plain cake, and you must write the flavour and the price on it yourself. That is tiring, and easy to forget.
What if the cake left the factory already complete?
In this part we meet the constructor, the word this, and the word static.
After this part, you will be able to:
- Write a constructor so that every object is born ready to use.
- Use
thisto tell a field apart from a parameter. - Use
staticfor data that all objects share.
Answer to the Part 11 Challenge
The question: x costs 10,000, y costs 20,000, and then the price of y is added to the price of x. What is the output?
The answer is C: 30000 20000.
Only cake x was changed. Cake y is a different object with its own content, so its price stays 20,000.
If you answered C, well done!
1. The Problem: Cakes That Are Not Complete
In Part 11, making one cake took three lines: new Kue(), fill in the flavour, fill in the price.
If you forget one line, the cake becomes Kue null, Rp0. The more fields there are, the bigger the chance that one is missed.
We need a way to make sure an object is always born ready to use.
2. A Constructor Is an Assembly Line
Picture an assembly line in a cake factory.
- The ingredients go in at the start: the flavour and the price.
- Along the line, the dough is shaped, filled and labelled.
- At the end, the cake comes out complete.
In Java, this assembly line is called a constructor. A constructor runs automatically every time we write new.
Click the diagram to open it in full size.
3. Writing a Constructor
Create a file named TokoKue.java:
class Kue {
String rasa;
int harga;
Kue(String rasa, int harga) {
this.rasa = rasa;
this.harga = harga;
}
void tampil() {
IO.println("Kue " + rasa + ", Rp" + harga);
}
}
void main() {
Kue a = new Kue("Cokelat", 15_000);
Kue b = new Kue("Keju", 18_000);
a.tampil();
b.tampil();
}
Kue Cokelat, Rp15000
Kue Keju, Rp18000
Look at two things in the line Kue(String rasa, int harga):
- The name of the constructor is exactly the same as the name of the class.
- There is no result type in front of it. Not even
void.
Making a cake now takes one line: new Kue("Cokelat", 15_000). The ingredients go into the assembly line, and the cake comes out complete.
4. The Word this: The Cake Being Made
Inside the constructor there is this line:
this.rasa = rasa;
Why are there two rasa? One is the field that belongs to the cake. The other is the parameter that just came in. They have the same name, so Java needs a hint.
The word this means: the cake that is being made right now.
| Code | Meaning |
|---|---|
this.rasa | The place for the flavour inside the cake |
rasa | The ingredient that just came in through the funnel |
So the line says: put the ingredient rasa into the flavour place of this cake.
5. The Word static: Owned by the Factory, Not by a Cake
What if we want to count how many cakes have been made?
That counter does not belong to one cake. It belongs to the factory. For this, Java has the word static.
Add two lines to the Kue class:
class Kue {
String rasa;
int harga;
static int jumlah = 0;
Kue(String rasa, int harga) {
this.rasa = rasa;
this.harga = harga;
jumlah++;
}
void tampil() {
IO.println("Kue " + rasa + ", Rp" + harga);
}
}
void main() {
Kue a = new Kue("Cokelat", 15_000);
Kue b = new Kue("Keju", 18_000);
a.tampil();
b.tampil();
IO.println("Kue yang sudah dibuat: " + Kue.jumlah);
}
Kue Cokelat, Rp15000
Kue Keju, Rp18000
Kue yang sudah dibuat: 2
Jumlah means "count", and the last line says "Cakes made so far: 2".
static int jumlah means: there is only one jumlah box, shared by all cakes. Every time the constructor runs, jumlah goes up by one. Because it belongs to the class, you read it through the class name: Kue.jumlah.
| Kind | How many are there? | Read it through |
|---|---|---|
A normal field, such as rasa | One per cake | The cake, for example a.rasa |
A static field, such as jumlah | One for all cakes | The class name: Kue.jumlah |
This is the meaning of static in the public static void main that we saw in Part 11. That long version of main belongs to the class, not to an object. So Java can run it without making an object first.
It also explains the error message in trap three of Part 11. tampil() is not static, so you must call it through a cake, not through the class name.
6. A Real Example: Savings Accounts
Create a file named Bank.java:
class Rekening {
String nama;
int saldo;
static int jumlah = 0;
Rekening(String nama, int saldoAwal) {
this.nama = nama;
this.saldo = saldoAwal;
jumlah++;
}
void setor(int uang) {
saldo += uang;
}
}
void main() {
Rekening rani = new Rekening("Rani", 50_000);
Rekening budi = new Rekening("Budi", 100_000);
rani.setor(20_000);
IO.println(rani.nama + ": " + rani.saldo);
IO.println(budi.nama + ": " + budi.saldo);
IO.println("Jumlah rekening: " + Rekening.jumlah);
}
Rani: 70000
Budi: 100000
Jumlah rekening: 2
Rekening means "bank account", saldo is the balance, saldoAwal is the starting balance, and setor means "deposit".
Rani deposits 20,000, so her balance is 70,000. Budi's balance does not change.
Every account has its own balance. But there is only one counter, and it belongs to the bank.
7. Three Constructor Traps
Trap one: forgetting this.
Kue(String rasa, int harga) {
rasa = rasa;
harga = harga;
}
Kue null, Rp0
javac does not complain. But without this, both rasa mean the parameter. The ingredient goes back into its own funnel, and the field of the cake stays empty.
Trap two: writing void in front of the constructor.
class Kue {
String rasa;
int harga;
void Kue(String rasa, int harga) {
this.rasa = rasa;
this.harga = harga;
}
}
void main() {
Kue a = new Kue("Cokelat", 15_000);
}
TokoKue.java:12: error: constructor Kue in class TokoKue.Kue cannot be applied to given types;
Kue a = new Kue("Cokelat", 15_000);
^
required: no arguments
found: String,int
reason: actual and formal argument lists differ in length
1 error
With void, it is not a constructor any more. It is a normal method that happens to have the same name. So the class Kue only has the empty constructor, which takes no ingredients.
TokoKue.Kue means the class Kue that lives inside the file TokoKue.java.
Trap three: new Kue() with no ingredients.
class Kue {
String rasa;
int harga;
Kue(String rasa, int harga) {
this.rasa = rasa;
this.harga = harga;
}
}
void main() {
Kue a = new Kue();
}
TokoKue.java:12: error: constructor Kue in class TokoKue.Kue cannot be applied to given types;
Kue a = new Kue();
^
required: String,int
found: no arguments
reason: actual and formal argument lists differ in length
1 error
Java gives you a free empty constructor only when you write none yourself. As soon as you write your own constructor, the free one is gone.
Read the lines required and found, as in Part 10. The constructor needs String,int, and it got nothing.
Summary
- A constructor is an assembly line that runs automatically on
new. Its name is the class name, and it has no result type. thismeans this object. Use it to tell a field apart from a parameter.staticmeans it belongs to the class, and all objects share it.
Challenge
Look at this code:
class Titik {
int x;
Titik(int x) {
x = x;
}
}
void main() {
Titik t = new Titik(5);
IO.println(t.x);
}
Titik means "point". What is the output?
- A.
0 - B.
5 - C. An error.
Try to answer before you run it. I explain the answer at the start of Part 13.
Conclusion & What's Next
Every object can now be born complete. If you remember only one thing, remember this.rasa = rasa;. The left side is the place inside the cake, and the right side is the ingredient that just came in.
In Part 13, we enter Java's engine room: a stack of trays, a big warehouse, and address tickets. There we learn why == can "lie".
See you in Part 13. Get your coffee ready. Hot chocolate is fine too.

Written by Muhammad Fari Madyan
Senior Backend Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

