01 / 21

Object Creation
and Storage

Master instantiation in Java: how the new keyword creates objects, how references connect to memory, and how constructors initialize state.

AP Computer Science A

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.

Class
The blueprint: defines what data and methods exist.
Object
A living instance: holds actual values in memory.

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:

1
Memory allocation: Space is reserved on the heap for the new object's instance variables.
2
Constructor call: The matching constructor runs, initializing the object's state.
3
Reference return: A reference (memory address) to the new object is returned and stored in the variable.

Anatomy of Instantiation

Dog myDog = new Dog("Rex", 5); Type Reference Variable Assign new Constructor Call ClassName varName = new ClassName(arguments);

References vs. Objects

STACK myDog @0x7a2f otherDog null HEAP Dog @0x7a2f name = "Rex" age = 5 breed = null null

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.

public class Dog { private String name; private int age; // Constructor public Dog(String n, int a) { name = n; age = a; } }

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.

public class Dog { private String name; private int age; // No-arg constructor public Dog() { name = "Unknown"; age = 0; } // Two-arg constructor public Dog(String n, int a) { name = n; age = a; } }

How to Instantiate Step by Step

1
Declare the reference: Choose the class type and a descriptive variable name.
2
Write new ClassName(...): Select the constructor that matches the data you want to pass in.
3
Supply arguments: Match the constructor's parameter types in the correct order.
4
Assign with =: Store the returned reference so you can use the object later.
5
Call methods: Use dot notation on the reference: myDog.getName().

Example 1: Basic Instantiation

// Step 1: Declare + instantiate Dog buddy = new Dog("Buddy", 3); // Step 2: Use the object System.out.println(buddy.getName()); // "Buddy" System.out.println(buddy.getAge()); // 3

Example 2: Multiple Objects

Dog a = new Dog("Rex", 5); Dog b = new Dog("Luna", 2); Dog c = a; // c and a point to the SAME object
STACK a b c HEAP Dog: "Rex", 5 @0x7a2f Dog: "Luna", 2 @0x3b10 alias!

Two collaborators work on one shared model

Two collaborators adjust one blue articulated model while a second model stands apart.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

References d1, d2 and d3 are traced through two distinct Dog objects and a null value.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

Dog ghost = null; // declared but NOT instantiated // This compiles, but at runtime: ghost.getName(); // NullPointerException!

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.

// Both are valid: String s1 = "Hello"; // String literal (pooled) String s2 = new String("Hello"); // Explicit object on heap
Literal
Java reuses the same object from the String pool if an identical literal already exists.
new String()
Always creates a new, separate object on the heap, even if the same text exists in the pool.

Common Mistakes

✕ Forgetting new
Writing Dog d = Dog("Rex", 5); without new causes a compile error. The constructor can only be called via new.
✕ Using null References
Calling a method on a null variable compiles fine but throws a NullPointerException at runtime.
✕ Wrong Argument Types
Passing a String where an int is expected (or vice versa) causes a compile error: no matching constructor found.

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?

Dog x = new Dog("A", 1); Dog y = new Dog("B", 2); Dog z = x;

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?