PrepGo

Object Creation and Storage (Instantiation) - AP Computer Science A Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 12 minutes to read.

Getting Started

In programming, a class serves as a blueprint for creating things. For example, a Car class can define the general properties (like color and speed) and behaviors (like startEngine()) of all cars. However, a blueprint isn't a car itself. To do useful work, we need to construct actual, individual Car objects from that blueprint. This process of creating objects is called instantiation, and it is fundamental to object-oriented programming in Java.

What You Should Be Able to Do

  • Create an object from a class using the new keyword and a constructor.

  • Explain that object variables store references to objects, not the objects themselves.

  • Trace the assignment of object references between variables.

  • Describe the purpose of the null reference and identify code that will cause a NullPointerException.

Key Concepts & Java Implementation

The Core Idea

Think of a class as a blueprint for a house. The blueprint defines that a house will have a certain number of bedrooms and a kitchen, but it's just a plan on paper. An object is an actual, physical house built from that blueprint. You can build many houses (objects) from the same blueprint (class).

In Java, when you create an object, it is built in the computer's memory. Your variable does not hold the entire object itself. Instead, it holds a reference, which is like the memory address of that object. You can think of a reference as the street address of the house.

If you have a variable house1 that holds the address of a new house, and you write house2 = house1, you are not building a second house. You are simply making a copy of the address and giving it to house2. Now, both house1 and house2 point to the exact same house object in memory. Any changes made to the house through the house2 reference (e.g., painting the door red) will be visible through the house1 reference, because there is only one house.

Finally, a reference variable can point to nothing. This special state is represented by the keyword null. A null reference is like having a piece of paper for an address that is completely blank.

Syntax & Implementation

To create an object, we use the new keyword followed by a call to the class's constructor. A constructor is a special method that is responsible for initializing the state of a new object. It often has the same name as the class itself.

Syntax Table

Keyword/SyntaxPurposeJava Example
newThe keyword that allocates memory for a new object.new Student("Ada", 12)
ConstructorA method-like block of code that initializes a newly created object.public Student(String name, int grade)
nullA literal representing a reference that does not point to any object.Student transferStudent = null;

Annotated Java Examples

First, let's define a simple Student class that we can create objects from.


// Student.java

public class Student {

    private String name;

    private int gradeLevel;


    // This is the constructor. It initializes a new Student object.

    public Student(String studentName, int studentGrade) {

        this.name = studentName;

        this.gradeLevel = studentGrade;

        System.out.println("New student object created for " + this.name);

    }


    public String getName() {

        return this.name;

    }

}

Now, let's see how to create and use Student objects in another class.


// School.java

public class School {

    public static void main(String[] args) {

        // 1. Declaration and Instantiation

        // The 'new' keyword creates the object.

        // The Student(...) part calls the constructor.

        // The 'studentA' variable holds the reference (address) to the object.

        Student studentA = new Student("Grace Hopper", 11);


        // 2. Declaring a reference without creating an object

        // 'studentB' does not point to any object yet. It is null by default

        // if it were an instance variable, but here we must initialize it.

        Student studentB = null;


        // 3. Reference Assignment

        // 'studentC' does not create a new object.

        // It copies the reference from 'studentA'. Both now point to the SAME object.

        Student studentC = studentA;


        System.out.println("Student A's name: " + studentA.getName()); // Prints "Grace Hopper"

        System.out.println("Student C's name: " + studentC.getName()); // Prints "Grace Hopper"

    }

}

Tracing & Analysis

Understanding how references work is critical for tracing code. Let's analyze the variables from the example above.

Execution Trace

  1. Student studentA = new Student("Grace Hopper", 11);

    • A new Student object is created in memory. Its name is "Grace Hopper".

    • The variable studentA now holds the memory address of this new object.

    • studentA ---> [Student object: name="Grace Hopper", gradeLevel=11]

  2. Student studentB = null;

    • The variable studentB is created.

    • It is explicitly set to null, meaning it holds no address and points to nothing.

    • studentB ---> null

  3. Student studentC = studentA;

    • The variable studentC is created.

    • The value inside studentA (the memory address) is copied into studentC.

    • No new Student object is created.

    • studentA ---> [Student object: name="Grace Hopper", gradeLevel=11]

    • studentC ---> [Student object: name="Grace Hopper", gradeLevel=11] (Points to the same object)

Analysis: The NullPointerException

What happens if you try to use a null reference as if it points to an object?


Student transferStudent = null;

// The line below will cause a crash (a runtime error).

System.out.println(transferStudent.getName());

This code attempts to call the getName() method on the object that transferStudent points to. But transferStudent points to nothing (null). Java cannot execute a method on a non-existent object, so it throws a NullPointerException, which is one of the most common errors in Java programming.

Java Syntax Quick-Reference

  • new ClassName(arguments): Creates a new instance (object) of the specified class, calling the matching constructor to initialize it.

  • null: A special literal that represents a reference variable that does not point to any object in memory.

Core Code Examples & Terminology

  • Class: A blueprint or template that defines the attributes (instance variables) and behaviors (methods) for a type of object.

  • Object: A specific instance of a class, with its own state (values for its instance variables), created in memory at runtime.

  • Instantiation: The process of creating an object from a class definition using the new keyword.

  • Constructor: A special method in a class that is called when an object is instantiated. Its primary job is to initialize the object's instance variables.

  • Reference Variable: A variable that stores the memory address of an object, rather than the object itself.

  • null: A reference value that indicates a variable does not point to any object.

  • Core Snippet 1: Object Instantiation

    
    // Creates a new String object and assigns its reference to 'greeting'.
    
    String greeting = new String("Hello, World!");
    

    This line declares a String reference variable greeting and instantiates a new String object for it to point to.

  • Core Snippet 2: Reference Assignment

    
    // 'anotherGreeting' now points to the exact same object as 'greeting'.
    
    String anotherGreeting = greeting;
    

    This line copies the reference (address) from greeting into anotherGreeting; no new object is created.

  • Core Snippet 3: Null Reference

    
    // 'message' is declared but does not point to any object.
    
    String message = null;
    

    This line declares a String reference variable message and explicitly initializes it to null.

Core Skill Check

  • Code Tracing: What is printed by the last line of this Java code?

    Student s1 = new Student("Alex", 9); Student s2 = new Student("Beth", 10); s2 = s1; System.out.println(s2.getName());

  • Debugging: Identify the runtime error in this Java code:

    Student newStudent = null; newStudent.getName();

  • Application: Write a single line of Java code that creates a Student object for a student named "Casey" in grade 12 and assigns it to a variable named senior.

Common Misconceptions & Errors

  1. Confusing the Object with its Reference. A variable like studentA is not the object; it's a pointer to the object. This distinction is crucial for understanding assignments and method calls.

  2. Thinking var2 = var1; Copies the Object. For objects, this assignment only copies the reference (the address). Both variables will now point to the same single object.

  3. Forgetting to Instantiate. Declaring a variable (Student s;) does not create an object. You must use the new keyword to instantiate it (s = new Student(...)) before you can call its methods.

  4. Dereferencing null. Attempting to call a method or access an instance variable using a reference that is null will always cause a NullPointerException at runtime. Always ensure an object reference is not null before using it.

Summary

Creating objects is the process of turning a class blueprint into a functional entity in your program. This is done using the new keyword, which allocates memory, and a constructor, which initializes the object's state. In Java, variables do not store objects directly but instead hold references—or memory addresses—that point to them. Understanding this reference-based system is essential, as it explains why assigning one object variable to another makes them both point to the same object and why using a null reference results in a NullPointerException. Mastering object creation and references is a foundational step in building complex, object-oriented applications.