Constructors in AP CSA: Complete Guide (2025-2026)

Constructors in AP CSA: Complete Guide (2025-2026)

Constructors are special methods that run automatically when an object is created, and they are among the most-tested concepts in AP CSA Unit 3. Every class you design will have a constructor, and the exam frequently tests parameter shadowing, constructor overloading, and what happens when no constructor is written.

What Is a Constructor?

A constructor initializes a newly created object. It is invoked automatically by the new keyword. Unlike regular methods, a constructor has no return type and its name must exactly match the class name (case-sensitive).

On the 2025-2026 AP CSA exam, constructors appear in Unit 3 (Class Creation, 10–18% of score). Expect FRQ parts that ask you to write a constructor, plus MCQs that test parameter shadowing and overloading.

Syntax and Rules

  • Same name as the class
  • No return type declared (not even void)
  • Can accept parameters (parameterized constructor)
  • If no constructor is written, Java provides a default no-arg constructor
  • Defining any constructor removes the implicit default constructor
  • Use this.fieldName = param to avoid parameter shadowing

Code Examples

Example 1: Basic Constructor with Parameter Shadowing Fix

What field values does this object have after construction?
public class Planet {
    private String name;
    private double mass;

    public Planet(String name, double mass) {
        this.name = name;   // this.name = the FIELD; name = the PARAMETER
        this.mass = mass;
    }

    public static void main(String[] args) {
        Planet p = new Planet("Mars", 6.39);
        System.out.println(p.name + " " + p.mass);
    }
}

Example 2: Constructor Overloading

Which constructor is called and what does rank print?
public class Player {
    private String alias;
    private int level;

    public Player(String alias) {
        this(alias, 1);          // delegates to the 2-param constructor
    }

    public Player(String alias, int level) {
        this.alias = alias;
        this.level = level;
    }

    public static void main(String[] args) {
        Player p = new Player("Zephyr");
        System.out.println(p.alias + " level " + p.level);
    }
}

Example 3: Conditional Validation in Constructor

What is printed when a negative value is passed?
public class Timer {
    private int seconds;

    public Timer(int seconds) {
        this.seconds = (seconds > 0) ? seconds : 0;
    }

    public static void main(String[] args) {
        Timer t1 = new Timer(30);
        Timer t2 = new Timer(-5);
        System.out.println(t1.seconds);
        System.out.println(t2.seconds);
    }
}

Common Pitfalls

1. Adding a Return Type

public void ClassName() looks like a constructor but is actually a method. The real constructor is never defined, so Java silently provides a default one—or if arguments are required, a compile error occurs on instantiation.

2. Parameter Shadowing

When the parameter name matches the field name, writing name = name reassigns the parameter to itself. Always use this.name = name.

3. Losing the Default Constructor

Once you write any constructor, Java no longer provides the no-arg default. Calling new MyClass() with no args then causes a compile error.

4. Calling this() from Non-First Position

this() (constructor chaining) must be the very first statement in the constructor body. Any code before it causes a compile error.

5. Thinking Constructors Set All Fields Automatically

Only fields explicitly set in the constructor are initialized to non-default values. Unset primitives default to 0/false; unset object references default to null.

AP Exam Tip: FRQ constructors are often worth 1–2 points. Graders check that your header is correct (no return type, exact class name), parameters match the specification, and fields are assigned using this. when needed.
⚠ Watch Out: The most common MCQ trap is a constructor that adds int or void as a return type. That method compiles but is never invoked as a constructor—the object gets Java's default initialization instead.

Check for Understanding

Score: 0 / 0 answered (8 total)
1. Which line contains a compile-time error? IDENTIFY the bug.
1: public class Vehicle {
2:   private int mileage;
3:   public int Vehicle(int m) {
4:     mileage = m;
5:   }
6: }
2. Consider the following statements about constructors. WHICH are true?

I. A constructor is automatically called when an object is created with new.
II. A class that defines a parameterized constructor retains its default no-arg constructor.
III. Constructors can call other methods of the same class.
3. What is printed when the following code executes? PREDICT before reading the options.
public class Box {
  private int depth;
  public Box() { depth = 5; setDepth(10); }
  public void setDepth(int d) { depth = d * 2; }
  public int getDepth() { return depth; }
}
// In main:
Box b = new Box();
System.out.println(b.getDepth());
4. A student writes the constructor below. WHICH problem does this code have?
public class Sensor {
  private double reading;
  public Sensor(double reading) {
    reading = reading;
  }
}
5. Consider these two class definitions. WHICH of the following object creations compiles without error?
class Alpha { public Alpha(int n) { } }
class Beta  { public Beta()    { } }

I. Alpha a = new Alpha();
II. Beta b = new Beta(7);
III. Beta b2 = new Beta();
6. What is the output of the code below? PREDICT before reviewing choices.
public class Counter {
  private int tally;
  public Counter() { this(1); }
  public Counter(int start) { tally = start; }
  public int getTally() { return tally; }
}
// In main:
Counter c = new Counter();
System.out.println(c.getTally());
7. WHICH of the following is true about a class that has NO explicitly defined constructor?
8. A class Profile has two fields: private String handle and private int rank. The constructor is:
public Profile(String h, int r) {
  handle = h;
  rank = (r > 0) ? r : 1;
}
What are handle and rank after new Profile(null, -3)?

Frequently Asked Questions

Yes. A constructor can call instance methods because the object exists by the time those lines run (it was created by new, and the constructor is initializing it). However, be careful: if the method relies on fields not yet set, you may get unexpected results.

The field is never updated. The assignment reads as 'set the local parameter equal to itself,' which has no effect. The field keeps its default value (0, false, or null). This is one of the top constructor bugs on the AP exam.

Yes. Private constructors appear in design patterns (like Singleton) but are not part of the 2025-2026 AP CSA curriculum. You will not be asked to write one on the exam, but you might see a private constructor in a reading and should know it prevents external instantiation.

Inheritance and super() calls are NOT part of the 2025-2026 AP CSA curriculum. You will not be tested on extends or super. Focus only on single-class constructor design.

Java matches arguments to parameters. new MyClass() with no args calls the no-arg constructor if one exists. If only a parameterized constructor exists, new MyClass() is a compile error.

1-on-1 Expert Support

Struggling with constructors, FRQ writing, or any Unit 3 topic? Get personalized help from an AP CSA teacher with 11+ years of experience and a 54% five-rate among students.

View Tutoring Options

Related Topics

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com