Constructors confuse many students because they look like methods, but act completely differently. Let's break down everything you need to know.
When a baby is born, a birth certificate is immediately created with initial data: Name, Weight, Date of Birth. You don't create a baby with 0 data and fill in their name 5 years later! A Constructor is the initial setup script that assigns valid starting values to an object at the exact second it is instantiated on the heap.
void!).new is executed. You cannot call a constructor manually later like a normal method (`obj.ConstructorName()` is illegal).| Type | How It Is Created | Code Example |
|---|---|---|
| Implicit Default Constructor | Inserted automatically by Java compiler ONLY IF you write zero constructors in your code. Sets fields to 0, null, or false. | public Person() {} (Invisible/auto-generated) |
| Explicit No-Arg Constructor | Written by you taking no parameters, setting default custom values. | public Person() { this.name = "Unknown"; } |
| Parameterized Constructor | Written by you taking parameters to set custom values upon object creation. | public Person(String n) { this.name = n; } |
Person p = new Person(); without parameters, a compile-time error occurs unless you explicitly wrote a no-arg constructor!
this() and super())Constructor Overloading means defining multiple constructors with different parameter signatures in the same class.
this(...): Calls another constructor in the **same class**.super(...): Calls a constructor in the **parent superclass**.this() or super() MUST be the **very first line** of code inside a constructor!Q: Consider class Book { public Book(String title) {} }. Does Book b = new Book(); compile? Why or why not?
Book(String title) was defined, Java deleted the automatic default constructor. Since no zero-argument constructor exists, new Book() fails at compile-time.