1.13

Object Creation and Storage (Instantiation)

Objects are created using the new keyword and a constructor whose signature must match the provided arguments.

1525% of exam
Understand It
Ace It
Context

What this topic is and why it exists

Imagine you're ordering a custom pizza.
You don't just shout "pizza!" into the void — you walk up to the counter, say "new pizza," and then specify exactly what you want: large, thin crust, extra cheese.
If the shop expects those three details in that exact order, you'd better not hand them "pepperoni, true, 42" and hope for the best.
That's essentially what object instantiation is in Java.
When you write `new Dog("Buddy", 3)`, you're telling Java to build a fresh `Dog` object by calling the constructor — a special method that shares the class's name — and passing it two arguments: a `String` and an `int`.
Java matches your call to the right constructor by checking the *signature*: the constructor name plus the ordered list of parameter types.
Get the order wrong, pass too many or too few arguments, or mix up the types, and Java won't know which constructor you mean.
It's like handing the pizza shop a shoe size instead of a crust type.
One more thing worth remembering: when you pass arguments, Java copies the values into the constructor's parameters.
The constructor uses those copies to set up your object's initial state.
And if you declare a variable like `Dog myDog;` without ever calling `new`, that variable holds `null` — it's pointing at nothing, an empty leash with no dog attached.
1 / 9