Muhammad Fari Madyan
Hire me
Java basics · part 12 of 21

Learn Java from Scratch Part 12: Constructors and this — The Assembly Line, this & static

The twelfth part of a Java series: a constructor as an assembly line so objects are born complete, the word this to tell a field from a parameter, the word static for data owned by the class, savings accounts, and three traps such as forgetting this.

Muhammad Fari Madyan

· 8 min read

Baca dalam Bahasa Indonesia →

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:

  1. Write a constructor so that every object is born ready to use.
  2. Use this to tell a field apart from a parameter.
  3. Use static for 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.

  1. The ingredients go in at the start: the flavour and the price.
  2. Along the line, the dough is shaped, filled and labelled.
  3. 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.

A constructor is like an assembly line: ingredients go in through new, this fills the fields, the cake comes out complete

Click the diagram to open it in full size.


3. Writing a Constructor

Create a file named TokoKue.java:

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();
}
TEXT
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:

JAVA
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.

CodeMeaning
this.rasaThe place for the flavour inside the cake
rasaThe 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:

JAVA
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);
}
TEXT
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.

KindHow many are there?Read it through
A normal field, such as rasaOne per cakeThe cake, for example a.rasa
A static field, such as jumlahOne for all cakesThe 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:

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);
}
TEXT
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.

JAVA
Kue(String rasa, int harga) {
    rasa = rasa;
    harga = harga;
}
TEXT
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.

JAVA
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);
}
TEXT
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.

JAVA
class Kue {
    String rasa;
    int harga;

    Kue(String rasa, int harga) {
        this.rasa = rasa;
        this.harga = harga;
    }
}

void main() {
    Kue a = new Kue();
}
TEXT
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

  1. A constructor is an assembly line that runs automatically on new. Its name is the class name, and it has no result type.
  2. this means this object. Use it to tell a field apart from a parameter.
  3. static means it belongs to the class, and all objects share it.

Challenge

Look at this code:

JAVA
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.

ShareLinkedInXWhatsApp

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.

Work with me
← part 11Classes and Objectspart 13 →Stack, Heap and References

Muhammad Fari Madyan

Senior Backend Engineer building real-time, high-concurrency systems that hold up in production. Co-founder of LampungDev. Based in Bandar Lampung, Indonesia (UTC+7), working remote.

Work

Case studiesCahTixExam platform, 4 stacksOpen source

© 2026 Muhammad Fari Madyan

Designed and built by Muhammad Fari Madyan