Welcome to the World of Objects!

In the previous chapters, we learned that a class is like a blueprint or a recipe. It tells the computer how to make something, but it isn't the thing itself. In this chapter, we are going to learn how to actually follow that blueprint to build something "real" in the computer's memory. This process is called instantiation.

Think of it this way: If a "Car" class is a blueprint for a vehicle, instantiation is the moment the factory actually builds a car you can drive. Don't worry if it sounds technical; by the end of this page, you'll be an expert at "bringing code to life."

What is Instantiation?

Instantiation is the act of creating an instance (an object) of a class. When we instantiate an object, the computer sets aside a dedicated space in its memory to hold all the data and information that belongs to that specific object.

The "new" Keyword

To create an object in Java, we use the keyword \( new \). This is the "magic button" that tells Java, "Hey! Go find some space in memory and build me a new object right now!"

The Constructor

When we use \( new \), we must follow it with a call to a constructor. A constructor is a special type of method that has the exact same name as the class. Its job is to "construct" the object and set its initial values.

Note: To see how to read the list of inputs a constructor needs, check out Chapter 1.9 on Method Signatures.

The Syntax: How to Write It

Creating and storing an object usually happens in one line of code. Let’s look at the three parts of this statement:

\( ClassName \space variableName = new \space ClassName(parameters); \)

  1. Declaration: \( ClassName \space variableName \) — This tells Java we are making a variable that is capable of "pointing to" an object of a specific type.
  2. Instantiation: \( new \space ClassName(...) \) — This is the actual creation of the object in memory.
  3. Assignment: \( = \) — This links the variable name to the newly created object.

Example:
Imagine we have a class called \( Dog \). To create a new dog object, we would write:
\( Dog \space myPet = new \space Dog("Fido"); \)

How Objects are Stored: References

This is where things get interesting! Unlike simple data types like \( int \) or \( double \) (which store the actual number), object variables work differently. An object variable stores a reference.

The "Remote Control" Analogy

Think of the object as a Television sitting across the room (in the computer's memory). The variable is the Remote Control in your hand. The remote control isn't the TV, but it "points to" the TV and allows you to control it.

  • The variable (Remote) holds the memory address of where the object (TV) is located.
  • We call this a reference variable.

The "null" Keyword

If a reference variable is not currently pointing to any object, its value is \( null \). It’s like having a remote control that isn't programmed to any TV yet. It exists, but it doesn't "refer" to anything.

Did you know?
If you try to use a variable that is \( null \) to do something (like calling a method), Java will get confused because there is no object to perform the action. This results in the famous "NullPointerException"! Always make sure your object is instantiated before you use it.

Step-by-Step: The Lifecycle of Object Creation

Let's look at exactly what happens inside the computer when we run this code:
\( String \space greeting = new \space String("Hello"); \)

  1. Space is Reserved: Java looks at the \( String \) class and sees how much memory a String needs.
  2. The Constructor Runs: The \( String \) constructor runs, using the value \( "Hello" \) to set up the object's internal data.
  3. The Object is Born: A new String object is placed at a specific address in memory (for example, Address \( \#502 \)).
  4. The Reference is Stored: The variable \( greeting \) is created, and it stores the value \( \#502 \). Now, whenever you use \( greeting \), Java knows exactly where to look!

Quick Review & Common Mistakes

Key Takeaways:
  • Instantiation is creating an object using the \( new \) keyword.
  • A constructor is called after \( new \) to initialize the object.
  • Reference variables store the memory address (a pointer) of the object, not the object itself.
  • \( null \) means a reference variable is pointing to nothing.
Common Student Pitfalls:
  • Forgetting "new": Writing \( Dog \space fido = Dog(); \) will cause a compiler error. You must use \( new \) to allocate memory.
  • Mismatched Parameters: If the constructor asks for a String and an Integer, you must provide them in that exact order, or the "factory" won't know how to build your object.
  • Confusing the Name and the Object: Remember that the variable name is just a label. You can have two different variables (two remotes) pointing to the same single object (one TV)!

Ready for the next step? Once you've created your object, head over to Chapter 1.14 to learn how to make it actually DO something by calling its methods!