The Stacks
UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.13

1.13 Object Creation and Storage (Instantiation)

new calls a constructor and returns a reference. Understanding that variables hold references — not objects — explains aliasing and every null crash.

What you need to know

  • ClassName var = new ClassName(args); — instantiation. The new keyword allocates memory, runs the constructor with the arguments, and returns a reference to the new object.
  • A constructor has the same name as the class and no return type. Constructors can be overloaded; the arguments determine which one runs.
  • A reference variable stores an address, not the object. Dog b = a; makes b refer to the same object as a — this is called aliasing. Changing the object through b is visible through a.
  • null is the value of a reference variable that doesn't refer to any object. Calling a method on a null reference throws a NullPointerException at run time.
  • Instance variables of reference type default to null if not initialized; a declared local variable has no default and must be assigned before use.
  • == on references compares addresses: true only if both refer to the same object. Two separate objects with identical contents are !=.

Worked example

Dog a = new Dog("Rex", 3);
Dog b = a;              // alias: same object
Dog c = new Dog("Rex", 3);
Dog d = null;

b.setAge(4);
System.out.println(a.getAge());    // 4  — a and b share the object
System.out.println(a == b);        // true
System.out.println(a == c);        // false — different objects
d.getAge();                        // NullPointerException
Exam tip: Draw boxes and arrows. Each new is a box; each variable is an arrow. Assignment between reference variables re-points an arrow — it never copies a box. If an arrow points to nothing (null) and you call a method through it, the program crashes.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • new does three things: allocates memory for the object, runs the constructor to initialize it, and returns a reference (the address). The variable on the left stores that reference.
  • Constructor overloading works like method overloading: the arguments' count and types select which constructor runs. new Dog() and new Dog("Rex", 3) call different constructors.
  • Aliasing is the single most important reference concept. After Dog b = a;, there is one Dog and two names for it. b.setAge(5) changes the Dog that a also points to. This surprises students every year.
  • Reassignment breaks the alias. b = new Dog("Bo", 1); makes b point to a new object; a still points to the original. Now there are two Dogs.
  • null means "points to nothing." It's a valid value for any reference variable. Calling any method on it — even toString, even equals — throws NullPointerException. Checking if (x == null) is safe (no method call).
  • == on references compares addresses. Two variables are == only if they alias the same object. Contents don't matter. For Strings and most classes, use .equals for content comparison.
  • Drawing it: boxes for objects, labeled arrows for variables. Assignment between reference variables redraws an arrow. It never duplicates a box.

Mistakes that cost points

  • Expecting Dog b = a; to make a copy. It doesn't. Same object.
  • Expecting a == b to be true for two separate objects with the same values. Different objects, different addresses, false.
  • Calling a method on null. If a question declares a reference variable and never assigns it (or assigns null), any method call on it crashes.
  • Writing public void ClassName() for a constructor. The void makes it a method, not a constructor. Constructors have no return type.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 Consider the following code, where Counter has a method increment() that adds 1 to its count and getCount() that returns it.
Counter x = new Counter();
Counter y = x;
x.increment();
y.increment();
System.out.println(x.getCount());
What is printed?
  1. A 0
  2. B 1
  3. C 2
  4. D A compile-time error
Show answer

Answer: C. x and y refer to the same Counter. Both increments apply to it, so the count is 2.

Q2 Which of the following will cause a NullPointerException at run time?
  1. A String s = ""; int n = s.length();
  2. B String s = null; int n = s.length();
  3. C String s = "abc"; s = null;
  4. D String s = null; s = "abc";
Show answer

Answer: B. Calling a method on a null reference throws NullPointerException. Assigning null (C, D) is fine; an empty String (A) is a valid object.

Key vocabulary

Instantiation
creating an object with new
Constructor
a special method that initializes a new object; same name as the class, no return type
Reference
the address of an object, stored in a reference variable
Aliasing
two reference variables referring to the same object
null
a reference value meaning 'no object'
NullPointerException
the run-time error from calling a method on null