Constructors and Overloading

Lesson 12 of 20

Give your objects a head start: constructors set fields up, and overloading lets one name wear many signatures.

The big picture

Born ready

A constructor is the first code an object ever runs. Overloading means several constructors compete for the call — Java picks the one whose parameter list fits.

Constructor = setup

Same name as the class, no return type. It runs exactly once, right after new.

Overloading

Several methods (or constructors) with one name but different parameter lists live side by side.

Java picks by args

new Box() matches the no-arg constructor; new Box(5) matches Box(int). No guessing.

Default fields first

Before any constructor line runs, every field starts at its default: 0, false or null.