Object Creation
and Storage
Master instantiation in Java: how the new keyword creates objects, how references connect to memory, and how constructors initialize state.
What Is an Object?
In Java, a class is a blueprint that defines the attributes (instance variables) and behaviors (methods) of a type. An object is a specific instance of that class: a concrete chunk of data living in memory at runtime.
The new Keyword
The new keyword is the only way to create objects in Java. When the JVM encounters new, three things happen in sequence:
Anatomy of Instantiation
References vs. Objects
What Are Constructors?
A constructor is a special method that runs when new is called. It has the same name as the class and no return type (not even void). Its job is to initialize instance variables.
Default vs. Parameterized
| Default Constructor | Parameterized Constructor | |
|---|---|---|
| Parameters | None | One or more |
| Provided by Java? | Yes (if no constructors written) | No |
| Field Values | Defaults (0, null, false) | Set by arguments |
| Example | new Dog() | new Dog("Rex", 5) |
Overloaded Constructors
A class can have multiple constructors with different parameter lists. Java picks the right one based on the number and types of arguments you pass.
How to Instantiate Step by Step
Example 1: Basic Instantiation
Example 2: Multiple Objects
Two collaborators work on one shared model
Two variables can provide access to one shared object. Copying a reference creates another access path; it does not duplicate the object.Follow object identity
Two new expressions create Fido and Rex. After the assignments, d1 refers to Rex, d3 refers to Fido and d2 is null. Both objects remain reachable; == compares reference identity, while equals depends on the class’s implementation.Example 3: null References
Primitive vs. Reference Types
| Primitive Types | Reference Types | |
|---|---|---|
| Examples | int, double, boolean | String, Dog, ArrayList |
| Stores | The actual value | A memory address |
| Default | 0, 0.0, false | null |
| Created with new? | No | Yes (except String literals) |
| Assignment copies... | The value itself | The address (alias) |
The String Exception
String is a reference type, but Java gives it a special shortcut: you can create a String without new by using a literal.
Common Mistakes
Instantiation Summary
| Concept | Key Point |
|---|---|
| Class | Blueprint that defines fields and methods |
| Object | Instance of a class, lives on the heap |
| new keyword | Allocates memory and calls the constructor |
| Constructor | Special method that initializes an object's state |
| Reference Variable | Stores the address of an object, lives on the stack |
| null | A reference that points to no object |
| Aliasing | Two references pointing to the same object |
Question 1 of 5
What does the new keyword do in Java?
Question 2 of 5
Consider the code below. How many Dog objects exist in memory after it runs?
Question 3 of 5
What happens if you call a method on a reference variable that is null?
Question 4 of 5
A class has only one constructor: Cat(String name, int age). Which line of code will cause a compile error?
Question 5 of 5
Where does a reference variable live, and where does the object it points to live?