How do you create an object with the new keyword and a constructor, and store it in a reference variable?
Topic 2.2 Creating and Storing Objects (Instantiation): use the new keyword to call a constructor and instantiate an object, choosing the correct constructor by its parameters, and store the result in a reference variable.
A focused answer to AP CSA Topic 2.2, covering the new keyword, constructors and how they initialise an object, choosing a constructor by its parameter list, matching argument types, storing the reference, and the meaning of null, with a worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 2.2) wants you to instantiate an object using the new keyword and a constructor, to choose the correct constructor by matching the arguments you supply to a constructor's parameter list, and to store the resulting reference in a variable. This is how every object (except literals like "hi") comes into existence.
Instantiation with new
The standard pattern has three parts:
ClassName variable = new ClassName(arguments);
ClassName variabledeclares a reference variable.newcreates the object.ClassName(arguments)calls a constructor, passing any required arguments.
For example, Rectangle r = new Rectangle(5.0, 2.0); builds a new Rectangle, initialises it with length 5.0 and width 2.0, and stores the reference in r.
Constructors and choosing the right one
A constructor has the same name as the class and no return type. Its parameters specify what information the object needs to start. A class often provides more than one constructor, each with a different parameter list - this is called overloading. For example:
Box() // no-argument constructor
Box(int width, int height) // two-argument constructor
Arguments must be assignable to the parameter types. Passing an int where a double is expected works (it widens), but passing a double where an int is expected does not compile, because narrowing needs a cast.
Storing the reference and null
The value produced by new is a reference to the new object, which you store in a reference variable. You can also declare a reference variable without creating an object; until you assign one, it holds null, meaning it refers to nothing.
Rectangle r3 = null; // valid: r3 refers to no object yet
Calling a method on a null reference throws a NullPointerException at run time, because there is no object to act on. This is one of the most common run-time errors in Java.
Try this
Q1. Write a statement that creates a Circle object with radius 7 using a constructor Circle(int radius) and stores it in c. [1 point]
- Cue.
Circle c = new Circle(7);
Q2. Explain what happens at run time if you call a method on a reference variable that is null. [2 points]
- Cue. A NullPointerException is thrown, because the variable refers to no object, so there is nothing for the method to act on.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. A class `Box` has two constructors: `Box()` and `Box(int width, int height)`. Which statement correctly creates a `Box` object that is 4 wide and 9 high and stores it?
(A) `Box b = Box(4, 9);`
(B) `Box b = new Box;`
(C) `Box b = new Box(4, 9);`
(D) `Box b = new Box(4.0, 9.0);`
(E) `new Box b = (4, 9);`
Show worked answer →
The answer is (C).
Creating an object requires the new keyword followed by a constructor call with arguments matching a constructor's parameter list. new Box(4, 9) matches Box(int width, int height) and is stored in the reference variable b. (A) omits new. (B) omits the parentheses, so it does not call a constructor. (D) passes double arguments, which do not match the int parameters. (E) is not valid Java syntax.
Markers reward the pattern ClassName var = new ClassName(arguments); with arguments matching a constructor.
AP 2020 (style)4 marksFree response (code writing). A class `Rectangle` has a constructor `Rectangle(double length, double width)`. Write a code segment that creates two `Rectangle` objects: `r1` with length 5.0 and width 2.0, and `r2` with length 3.0 and width 3.0. Then declare a third reference variable `r3` of type `Rectangle` that does not yet refer to any object.
Show worked answer →
A 4-point question testing instantiation and the null reference.
Rectangle r1 = new Rectangle(5.0, 2.0);
Rectangle r2 = new Rectangle(3.0, 3.0);
Rectangle r3 = null;
Point 1: new Rectangle(5.0, 2.0) calls the constructor with matching double arguments and stores the reference in r1. Point 2: a second, independent object for r2. Point 3: arguments in the correct order (length then width). Point 4: r3 declared as type Rectangle and set to null (or just declared) so it refers to no object yet. Forgetting new, or swapping the argument order, loses marks.
Related dot points
- Topic 2.1 Objects: Instances of Classes: explain the relationship between a class and its objects, and describe an object as an instance of a class with state and behavior.
A focused answer to AP CSA Topic 2.1, covering the class-object relationship, what it means for an object to be an instance, the difference between attributes (state) and methods (behavior), and reference versus primitive variables, with a worked example.
- Topic 2.3 Calling a Void Method: call a non-static void method on an object using dot notation, and explain that a void method performs an action but returns no value.
A focused answer to AP CSA Topic 2.3, covering dot notation for calling methods on objects, what void means, why a void call cannot be used in an expression, the difference between a method signature and a call, and method side effects, with a worked example.
- Topic 2.4 Calling a Void Method with Parameters: call a void method that takes parameters, passing arguments that match the parameter list in number, order and type, and explain how arguments are copied to parameters.
A focused answer to AP CSA Topic 2.4, covering parameters versus arguments, matching the argument list in number, order and type, pass-by-value for primitives, overloaded methods, and how the call supplies data to the method, with a worked trace.
- Topic 2.5 Calling a Non-void Method: call a method that returns a value, and use the returned value by storing it, printing it, or including it in an expression.
A focused answer to AP CSA Topic 2.5, covering methods that return a value, the return type, using a returned value in an assignment, expression or print, the difference from a void method, and chaining method calls, with a worked trace.
- Topic 2.6 String Objects: Concatenation, Literals, and More: create String objects from literals or a constructor, concatenate strings with the + operator, and predict the result of mixing strings with numbers and escape sequences.
A focused answer to AP CSA Topic 2.6, covering String literals and the String constructor, concatenation with the plus operator, the rule that any value concatenated with a String becomes a String, left-to-right evaluation traps, and escape sequences, with a worked trace.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)