BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

Classes, objects and constructors

A class is a blueprint; an object is one thing built from it with new.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

A class is the architect's plan for a house, an object is one house built from the plan, and the constructor is the checklist the builders follow before handing over the keys.

What happens, in order
  1. 1A constructor has the class name and no return type: Order(String id) { this.id = id; }this.id is the field, id is the parameter that shadows it.
  2. 2If you write no constructor, Java adds an empty oneThe moment you write any constructor, that free one disappears.
  3. 3Constructors can call each other with this(...), and a subclass constructor calls its parent with super(...) as its very first statementIf you do not write it, Java inserts super().
  4. 4Fields you do not set get defaults: 0, false, or nullFinal fields must be assigned exactly once by the end of every constructor.
Remember
Watch out
Assigning a parameter to itself (id = id;) instead of this.id = id;, so the field stays nullCalling an overridable method from a constructorForgetting that a class with only a parameterised constructor cannot be created with new Foo()
Example (highlighted lines explain it)
public class Order {  private final String id;  private int quantity;  public Order(String id) { this(id, 1); }  public Order(String id, int quantity) {    this.id = java.util.Objects.requireNonNull(id);    this.quantity = quantity;  }}
Read the full explanation (about 2 min)

A class is a blueprint; an object is one thing built from it with new. A constructor is the special method that runs while the object is being built and puts it into a valid first state.

A mental model

A class is the architect's plan for a house, an object is one house built from the plan, and the constructor is the checklist the builders follow before handing over the keys.

How it works

A constructor has the class name and no return type: Order(String id) { this.id = id; }. this.id is the field, id is the parameter that shadows it.

If you write no constructor, Java adds an empty one. The moment you write any constructor, that free one disappears.

Constructors can call each other with this(...), and a subclass constructor calls its parent with super(...) as its very first statement. If you do not write it, Java inserts super().

Fields you do not set get defaults: 0, false, or null. Final fields must be assigned exactly once by the end of every constructor.

Going deeper

A constructor should leave the object fully usable. Letting it call overridable methods is a classic trap: the subclass method runs before the subclass fields are set.

Many parameters make a constructor hard to read and easy to get wrong (new Pizza(true, false, 3, 2)). That is when a builder, a factory method or a record is a better answer.

Validate in the constructor (Objects.requireNonNull, range checks) so an invalid object can never exist; every method afterwards can then trust the fields.

Under the hood

new Order("a") allocates zeroed memory on the heap, then runs the constructor bytecode (<init>). Field initialisers and instance initialiser blocks are copied into the start of every constructor that calls super(...).

Common mistakes

  • Assigning a parameter to itself (id = id;) instead of this.id = id;, so the field stays null.
  • Calling an overridable method from a constructor.
  • Forgetting that a class with only a parameterised constructor cannot be created with new Foo().

How Unravel recognises it

When you paste code, Unravel looks for these signs of classes, objects and constructors. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is Classes, objects and constructors?

A class is a blueprint; an object is one thing built from it with new. A constructor is the special method that runs while the object is being built and puts it into a valid first state.

What are the most common mistakes with Classes, objects and constructors?

Assigning a parameter to itself (id = id;) instead of this.id = id;, so the field stays null. Calling an overridable method from a constructor. Forgetting that a class with only a parameterised constructor cannot be created with new Foo().

How can I see Classes, objects and constructors in real code?

Paste your own Java into Unravel and it will point out where classes, objects and constructors appears, explain how it works at the detail level you choose, and draw the flow of the code around it.