MA-JAVA0 · Module 8
Constructors, overloading, and static
Topics: Constructors · this · static · Constants and utilities
Overview
Session 8 — Constructors, overloading, and static members
You will initialise objects reliably with constructors, chain work with this(...), overload constructors for convenience, and place shared constants and utility methods in static members.
Time: about 90 minutes.
Concepts
1. Constructors run on new
A constructor has the same name as the class and no return type. Java invokes it immediately after memory allocation:
public class Window {
private final int width;
private final int height;
public Window(int width, int height) {
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("dimensions must be positive");
}
this.width = width;
this.height = height;
}
public int area() {
return width * height;
}
}
final fields must be assigned exactly once by the end of every constructor path.
2. Overloading
Multiple constructors differ by parameter lists:
public Window(int side) {
this(side, side); // delegate to primary constructor
}
The call this(...) must be the first statement in a constructor.
3. static fields and methods
static data belongs to the class, not each instance—ideal for shared counters or constants:
public class Version {
public static final String CODE = "1.3.0";
}
static methods may only touch static data directly (they lack this).
4. No such thing as a "static constructor"
A constructor has the class name, no return type, and is invoked by new. It can never be marked static — a constructor runs on a freshly allocated object, so a "static constructor" does not exist in Java.
Keep static for data and behaviour that belong to the class itself (constants, counters, pure helpers like Math.max). Do not use static methods to build objects — reach for the constructor and new instead:
public static final int DEFAULT_SIZE = 16; // shared constant
If you see public static SomeClass make(...), that is a factory method — a real but advanced idiom you will meet much later; nothing in this course requires it.
5. Initialiser blocks (mention only)
Instance initialisers { ... } run before constructors—rare in beginner code; prefer constructors until you read style guides recommending them.
Examples
Example — colour palette record-style manual class
public class Rgb {
private final int r;
private final int g;
private final int b;
public Rgb(int r, int g, int b) {
this.r = clamp(r);
this.g = clamp(g);
this.b = clamp(b);
}
private static int clamp(int channel) {
if (channel < 0) {
return 0;
}
if (channel > 255) {
return 255;
}
return channel;
}
public String css() {
return "rgb(" + r + "," + g + "," + b + ")";
}
}
clamp is a private static helper — it needs no object state, so it belongs to the class rather than to any instance. Objects are still built with new Rgb(...); no static method constructs them.
Bridge
Constructors ↔ object lifecycle
-
Spring / Guice frameworks ultimately call your constructors—understand them before relying on dependency injection magic.
-
Serialization courses discuss default vs custom constructors—clarity now prevents surprises later.
-
Testing builds objects through constructors and asserts on their behaviour — you rarely need factories at this level.
-
Immutability (
finalfields) pairs with constructors that establish complete state up front.
When constructors validate aggressively, downstream code trusts inputs more.
Practice check — constructors, overloading, and static
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.When is a constructor called?
2.What does `this.x = x;` usually mean inside a constructor?
3.Method overloading means…
4.A `static` method can directly access…
5.If you write no constructor, Java provides…