Constructors
A constructor is the method-like block that runs when you write new. It sets the new object's fields to a valid starting state before any caller can use the instance.
Detailed Explanation
Objects should never exist in a half-ready form. A constructor is the gate. Parameters come in, rules run, fields are assigned, and only then does new return a reference. If a payment needs a non-blank reference and a positive amount, those checks belong in the constructor, not in a later setup() call that callers might forget.
Java supplies a default no-argument constructor only when you write none. The moment you declare any constructor, the default disappears. That is why adding Payment(String, long) suddenly breaks new Payment() at every call site. The compiler is telling you the class no longer has an empty starting state.
A constructor has the class name, no return type, and may be overloaded. One overload can call another with this(...) as its first statement. That is constructor chaining: shared validation lives in one place, convenience overloads forward to it. A subclass constructor must call super(...) or an implicit super() before it touches its own fields, because the superclass part of the object is built first.
Initialisation order is fixed. After allocation, fields get language defaults. Then instance field initializers and instance initializer blocks run, in source order. Then the constructor body runs. Static initializers have already run the first time the class was used. Knowing this order explains why a field initializer can see this but cannot assume a subclass constructor has finished.
You reach for a constructor when an object is invalid without certain values. You reach for a static factory (Payment.ofRupees(...)) when creation needs a name, caching, or a subtype. You reach for a builder when there are many optional parameters and a telescoping list of constructors would be unreadable.
Constructors cannot be abstract, static, or final in the method sense, and they are not inherited. A subclass does not get the superclass constructors; it must declare its own and call up. Access modifiers still apply: a private constructor blocks new from outside the class, which is how singletons and utility classes prevent stray instances.
The cost of a heavy constructor is hidden work at every new. Database calls, file I/O, and thread starts inside a constructor make objects hard to test and easy to leak if construction throws halfway. Keep constructors cheap: assign fields, validate, return. Do the rest in a factory or an explicit open() method.
Code Examples
public class Ticket {
private final String id;
private final int seat;
// The one constructor that actually validates.
public Ticket(String id, int seat) {
if (id == null || id.isBlank()) {
throw new IllegalArgumentException("id required");
}
if (seat < 1) {
throw new IllegalArgumentException("seat must be >= 1");
}
this.id = id;
this.seat = seat;
}
// Convenience overload: chain to the real constructor.
public Ticket(String id) {
this(id, 1);
}
public String id() {
return id;
}
public static void main(String[] args) {
Ticket t = new Ticket("A-17", 12);
System.out.println(t.id()); // prints: A-17
}
}public class Defaults {
private final int n = 3; // field initializer runs before the constructor body
public Defaults() {
System.out.println(n); // prints: 3
}
public static void main(String[] args) {
new Defaults();
}
}Common Gotchas
Declaring any constructor removes the default
`new Foo()` fails once `Foo(int x)` exists, unless you also write `Foo()`.
`this(...)` must be first
you cannot validate or log before chaining. Put shared checks in the target constructor.
Throwing from a constructor leaves no object
the caller never receives a reference, but side effects already performed (a started thread, a written file) are not rolled back.
Records generate a canonical constructor
you can compact-customize it, but the component list still defines the state.