---
title: "AP CSA 3.4: Constructors"
description: "Review AP Computer Science A 3.4, including Java constructors, instance variables, object state, default constructors, default values, parameters, and mutable object copies."
canonical: "https://fiveable.me/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk"
type: "study-guide"
subject: "AP Computer Science A"
unit: "Unit 3 – Class Creation"
lastUpdated: "2026-06-09"
---

# AP CSA 3.4: Constructors

## Summary

Review AP Computer Science A 3.4, including Java constructors, instance variables, object state, default constructors, default values, parameters, and mutable object copies.

## Guide

A constructor sets the starting state of an [object](/ap-comp-sci-a/key-terms/object "fv-autolink") by giving values to its instance variables when you use `new`. When a constructor runs, memory is allocated for the object and a reference to that object is returned. For [AP Computer Science A](/ap-comp-sci-a "fv-autolink"), check that constructors have the class name, no return type, and enough information to initialize the object's fields.

## Why This Matters for the AP Computer Science A Exam

[Class creation](/ap-comp-sci-a/unit-3 "fv-autolink") shows up in both multiple-choice and free-response on the AP Computer Science A exam, and constructors are the part that gets your object ready to use. On the free-response class design question, you are given a specification table and asked to build a class with a header, [private](/ap-comp-sci-a/key-terms/private "fv-autolink") instance variables, constructors, and methods. If your constructor does not initialize every instance variable correctly, the rest of your methods can fail. You also need to trace code that creates objects and predict the state of those objects, which means knowing exactly what a constructor sets and what default values appear when nothing is assigned.

## Key Takeaways

- A constructor has the same name as the class and has no return type, not even `void`.
- A constructor should initialize every instance variable so the object starts in a valid state.
- Constructor parameters provide the data used to set instance variables; without [parameters](/ap-comp-sci-a/unit-4/recursion/study-guide/p4D3YegZCLwQ3KJVvsd4 "fv-autolink"), you set fixed starting values.
- If you write no constructor, Java supplies a default no-parameter constructor and sets instance variables to defaults: `int` is 0, `double` is 0.0, `boolean` is false, and any [reference type](/ap-comp-sci-a/key-terms/reference-type "fv-autolink") is [null](/ap-comp-sci-a/key-terms/null "fv-autolink").
- When a constructor [parameter](/ap-comp-sci-a/key-terms/parameter "fv-autolink") is a [mutable object](/ap-comp-sci-a/key-terms/mutable-object "fv-autolink"), store a copy of it so outside code cannot change your object's internal state later.
- Use `this.variableName` when a parameter has the same name as an instance variable so you assign to the right one.

## Core Concepts

### What a Constructor Does

A constructor sets the initial state of an object. State means the object's [attributes](/ap-comp-sci-a/key-terms/attribute "fv-autolink") and their values at a given time, defined by its instance variables. An object has a has-a relationship with its instance variables, so a `Student` object has a name, a grade level, and a GPA.

When a constructor is called with `new`, memory is allocated for the object and a reference to it is returned and stored in your [variable](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink").

```java
public class Student {
    private String name;
    private int gradeLevel;
    private double gpa;

    // Constructor - same name as class, no return type
    public Student(String studentName, int grade) {
        name = studentName;
        gradeLevel = grade;
        gpa = 0.0;
    }
}

// Using the constructor
Student alice = new Student("Alice", 11);  // Constructor runs, reference stored in alice
```

Three things to notice: the constructor name matches the class name, there is no return type, and it initializes the instance variables.

### Parameterized vs No-Parameter Constructors

A parameterized constructor takes parameters and uses them to set instance variables, so you can create an object with meaningful values right away.

```java
public class Timer {
    private int seconds;
    private boolean isRunning;

    public Timer(int initialSeconds) {
        seconds = initialSeconds;
        isRunning = false;
    }
}

Timer timer = new Timer(60);  // Starts at 60 seconds
```

A constructor with no parameters sets fixed starting values instead.

```java
public class Timer {
    private int seconds;
    private boolean isRunning;

    public Timer() {
        seconds = 0;
        isRunning = false;
    }
}

Timer timer = new Timer();  // Starts at 0 seconds
```

### The Default Constructor and Default Values

If you do not write any constructor, Java provides a no-parameter constructor called the default constructor. It sets each instance variable to the default value for its type:

| Attribute type | Default value |
|:---|:---|
| `int` | 0 |
| `double` | 0.0 |
| `boolean` | false |
| reference type (such as `String` or `ArrayList`) | null |

```java
// No constructor written
public class Point {
    private int x;
    private int y;
    // Java provides: public Point() { }  -> x is 0, y is 0
}

Point p = new Point();  // This works
```

Once you write your own constructor, Java does not provide the default one. So if you write a parameterized constructor and still want to create objects with no arguments, you must write a no-parameter constructor yourself.

```java
public class Point {
    private int x;
    private int y;

    public Point(int xCoord, int yCoord) {
        x = xCoord;
        y = yCoord;
    }
}

Point p1 = new Point(3, 4);  // Works
// Point p2 = new Point();   // Will not compile - no no-parameter constructor

```

### Initializing Reference Type Instance Variables

Reference type instance variables default to null. If a method tries to use a null collection, you get a `NullPointerException`. Initialize collections inside the constructor so the object is ready to use.

```java
public class Student {
    private String name;
    private ArrayList<String> courses;

    public Student(String studentName) {
        name = studentName;
        courses = new ArrayList<>();  // Create the list so it is not null
    }

    public void addCourse(String course) {
        courses.add(course);  // Works because courses was initialized
    }
}
```

### Copying Mutable Constructor Parameters

When a constructor parameter is a mutable object, such as an `ArrayList`, storing the parameter directly means your instance variable and the outside variable point to the same object. Outside code could then change your object's internal state. To prevent this, initialize your instance variable with a copy of the parameter.

```java
public class Roster {
    private ArrayList<String> students;

    public Roster(ArrayList<String> incoming) {
        // Store a copy, not the same reference
        students = new ArrayList<>(incoming);
    }
}
```

Now changes to the original `incoming` [list](/ap-comp-sci-a/key-terms/list "fv-autolink") outside the class do not affect the `students` list inside the object.

### Handling Parameter and Instance Variable Name Conflicts

When a parameter has the same name as an instance variable, the name refers to the parameter inside the constructor. Use `this.` to assign to the instance variable.

```java
public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;  // this.name is the instance variable
        this.age = age;    // this.age is the instance variable
    }
}
```

Without `this.`, writing `name = name;` just assigns the parameter to itself and leaves the instance variable unchanged. You could also avoid the conflict by using different parameter names.

## How to Use This on the AP Computer Science A Exam

### Free Response

On the class design free-response question, you build a class from a specification. Your constructor should:

- Match the class name and have no return type.
- Assign every [private instance variable](/ap-comp-sci-a/key-terms/private-instance-variable "fv-autolink") so the object starts in a valid state.
- Use the constructor parameters listed in the specification to set the matching instance variables.
- Store a copy of any mutable object parameter when the specification expects the object to be independent.

Build the constructor before the methods, because your methods rely on the instance variables it sets.

### Code Tracing

For multiple-choice and [tracing](/ap-comp-sci-a/key-terms/tracing "fv-autolink") questions:

- When you see `new ClassName(args)`, follow the matching constructor and track what each instance variable becomes.
- If an instance variable is never assigned and no constructor sets it, apply the default value: 0 for `int`, 0.0 for `double`, false for `boolean`, null for a reference type.
- Watch for a parameter that shares a name with an instance variable. Without `this.`, the instance variable does not change.

### Common Trap

A method like `public void Student()` with a return type is a regular method, not a constructor, even though its name matches the class. A real constructor has no return type at all.

## Common Misconceptions

- A constructor is not a normal method you call by name. It runs when you use `new`, and it never has a return type, not even `void`.
- Writing `void` before something with the class's name makes it an ordinary method, so Java still uses the no-argument default constructor and your "constructor" never runs on creation.
- The default constructor only exists when you write no constructors at all. Once you write any constructor, you must add a no-parameter one yourself if you want it.
- Default values are not blank or undefined. Numbers start at 0 or 0.0, booleans start at false, and reference types start at null until you assign them.
- A [null reference](/ap-comp-sci-a/key-terms/null-reference "fv-autolink") type instance variable is not an empty `ArrayList`. You must create the object with `new` before calling methods on it, or you get a `NullPointerException`.
- Storing a [mutable](/ap-comp-sci-a/unit-4/arraylist-methods/study-guide/8juIkbLwLGELYtblBeCf "fv-autolink") parameter directly is not the same as copying it. Without a copy, outside code still shares the same object and can change your [object's state](/ap-comp-sci-a/key-terms/objects-state "fv-autolink").

## Related AP Computer Science A Guides

- [3.8 Scope and Access](/ap-comp-sci-a/unit-3/scope-and-access/study-guide/56FUK4RSofr7slzwm6xm)
- [3.3 Anatomy of a Class](/ap-comp-sci-a/unit-3/anatomy-of-a-class/study-guide/DcGY5KOyK98H9Fn2w8jh)
- [3.9 This Keyword](/ap-comp-sci-a/unit-3/this-keyword/study-guide/Zste3M7m756uzwR0zCQK)
- [3.7 Static Variables and Methods](/ap-comp-sci-a/unit-3/static-variables-and-methods/study-guide/zzhHVbXBRCZQ7ng3EeWX)
- [3.5 Writing Methods](/ap-comp-sci-a/unit-3/writing-methods/study-guide/rtuMpRFmidkpYTzvDndS)
- [3.1 Abstraction and Program Design](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz)

## Vocabulary

- **attribute**: The data or properties that define the state of an object or class.
- **constructor**: A special method that is called to create and initialize an object of a class, having the same name as the class.
- **constructor parameters**: Data passed to a constructor that is used to initialize instance variables with specific values.
- **default constructor**: A no-parameter constructor automatically provided by Java when no constructor is explicitly written; initializes instance variables to default values.
- **default values**: The initial values automatically assigned to instance variables by the default constructor based on their data type (0 for int, 0.0 for double, false for boolean, null for reference types).
- **has-a relationship**: A relationship between an object and its instance variables, indicating that an object has certain attributes.
- **instance variable**: A variable that belongs to an object and can be accessed throughout the class, as opposed to a local variable that is limited to a specific block of code.
- **mutable object**: An object whose state can be changed after it is created; when used as a constructor parameter, a copy should be made to prevent external modification.
- **object's state**: The current values of all instance variables belonging to an object at a given time.

## FAQs

### What is AP CSA 3.4 about?

AP Computer Science A 3.4 is about constructors. Constructors set the initial state of an object by initializing its instance variables when the object is created with new.

### What is a constructor in Java?

A constructor is a special block of code with the same name as the class and no return type. It runs when an object is created and should initialize the instance variables that define the object's state.

### What is the default constructor in Java?

If you write no constructor, Java provides a no-parameter default constructor. It initializes instance variables to type defaults: int to 0, double to 0.0, boolean to false, and reference types to null.

### What happens if I write my own constructor?

Once you write any constructor, Java no longer automatically provides the no-parameter default constructor. If you still want to create objects with no arguments, you must write a no-parameter constructor yourself.

### Why should mutable constructor parameters be copied?

If a constructor receives a mutable object, the instance variable should store a copy instead of the original reference. That prevents outside code from changing the original object and unexpectedly changing the new object's internal state.

### How do constructors show up on the AP CSA exam?

Constructors appear in class-writing and code-tracing questions. Be ready to write constructors that initialize every instance variable, trace object creation with new, and recognize default values when no explicit assignment is made.

## Structured Data

```json
{"@context":"https://schema.org","@type":"FAQPage","inLanguage":"en","mainEntity":[{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk#what-is-ap-csa-34-about","name":"What is AP CSA 3.4 about?","acceptedAnswer":{"@type":"Answer","text":"AP Computer Science A 3.4 is about constructors. Constructors set the initial state of an object by initializing its instance variables when the object is created with new."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk#what-is-a-constructor-in-java","name":"What is a constructor in Java?","acceptedAnswer":{"@type":"Answer","text":"A constructor is a special block of code with the same name as the class and no return type. It runs when an object is created and should initialize the instance variables that define the object's state."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk#what-is-the-default-constructor-in-java","name":"What is the default constructor in Java?","acceptedAnswer":{"@type":"Answer","text":"If you write no constructor, Java provides a no-parameter default constructor. It initializes instance variables to type defaults: int to 0, double to 0.0, boolean to false, and reference types to null."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk#what-happens-if-i-write-my-own-constructor","name":"What happens if I write my own constructor?","acceptedAnswer":{"@type":"Answer","text":"Once you write any constructor, Java no longer automatically provides the no-parameter default constructor. If you still want to create objects with no arguments, you must write a no-parameter constructor yourself."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk#why-should-mutable-constructor-parameters-be-copied","name":"Why should mutable constructor parameters be copied?","acceptedAnswer":{"@type":"Answer","text":"If a constructor receives a mutable object, the instance variable should store a copy instead of the original reference. That prevents outside code from changing the original object and unexpectedly changing the new object's internal state."}},{"@type":"Question","@id":"https://fiveable.me/ap-comp-sci-a/unit-3/constructors/study-guide/3Ez6zzak2wRwMrTj2ZQk#how-do-constructors-show-up-on-the-ap-csa-exam","name":"How do constructors show up on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"Constructors appear in class-writing and code-tracing questions. Be ready to write constructors that initialize every instance variable, trace object creation with new, and recognize default values when no explicit assignment is made."}}]}
```
