Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. Instantiation with new
  3. Constructors and choosing the right one
  4. Storing the reference and null
  5. Try this

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 variable declares a reference variable.
  • new creates 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

Sources & how we know this