Dove's LibraryDove's Library

3.4 - Constructors

An object’s state is just its attributes and their current values at any given moment, and that state is defined by its instance variables. This creates what’s called a has-a relationship between the object and its instance variables (a Dog has a name, for instance).
A constructor sets up an object’s initial state, which should give starting values to every instance variable. When you call a constructor, Java allocates memory for the object and hands back a reference to it. If the constructor has parameters, those provide the data used to initialize the instance variables.
public class Dog { private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } } Dog myDog = new Dog("Rex", 3); // constructor sets initial state
Here’s what happens, step by step, when Java runs the last line above:
  1. The keyword new sets aside memory for a brand new Dog object.
  1. The constructor runs with the parameters name and age holding Rex and 3, and stores them in the object’s instance variables.
  1. A reference to the new object is returned and stored in myDog.
Notice that a constructor has no return type, not even void. That’s one way to tell it apart from a method.
If a constructor parameter is a mutable object (one that can be changed after creation), you should initialize the instance variable with a copy of that object, not the original reference. That way, changes made through your object’s methods don’t accidentally mess with the original object someone else is holding onto.
Example: an Owner class that stores a Dog. Dog objects are mutable (they have a setName method), so the constructor makes its own copy instead of keeping the caller’s Dog. This assumes Dog has getName, getAge, and setName methods like the ones in 3.5.
public class Owner { private Dog pet; public Owner(Dog d) { pet = new Dog(d.getName(), d.getAge()); // a new Dog with the same state as d } public String getPetName() { return pet.getName(); } }
Dog rex = new Dog("Rex", 3); Owner sam = new Owner(rex); rex.setName("Max"); System.out.println(rex.getName()); // Max System.out.println(sam.getPetName()); // Rex, sam's copy didn't change
If the constructor had used pet = d; instead, both lines would print Max, since rex and pet would refer to the same object.
If you don’t write a constructor at all, Java automatically provides a no-parameter one called the default constructor, and it sets every instance variable to a default value based on its type:
Type
Default Value
int
0
double
0.0
boolean
false
reference type
null
public class Dog { private String name; private int age; // no constructor written, Java provides the default one } Dog mysteryDog = new Dog(); // mysteryDog.name is null, mysteryDog.age is 0
Two things to watch for:
  • Once you write any constructor, Java no longer gives you the default one. If you still want to create an object with no arguments, you have to write a no-parameter constructor yourself.
  • Any instance variable a constructor doesn’t set still starts with its default value from the table above.
public class Dog { private String name; private int age; public Dog(String n) { name = n; // age is never set here, so it keeps its default value, 0 } } Dog a = new Dog("Rex"); // works, and a's age is 0 Dog b = new Dog(); // compile error: Dog no longer has a no-parameter constructor
To make new Dog() work again, add your own no-parameter constructor to the class. A class can have more than one constructor as long as their signatures are different (see 1.13).
public Dog() { name = "Unknown"; age = 0; }

AP® is a registered trademark of the College Board, which is not affiliated with Dove's Library and was not involved in the production of, and does not endorse, this website.

Providing free, high-quality AP notes to help students succeed, earn college credit, and reach their educational goals, ensuring everyone has access to the resources they need.

© 2026 Standard Learning Group, Inc., which operates Dove's Library. All rights reserved.