AP CSA Constructors

Constructors in AP CSA

A constructor is a special method that runs automatically when an object is created with new. Its job is to initialize the object’s instance variables to valid starting values.

Constructor Rules

  • Same name as the class
  • No return type (not even void)
  • Called automatically when new ClassName() is used
  • A class can have multiple constructors (overloading)
public class Counter {
    private int count;

    // Constructor -- no return type, same name as class
    public Counter() {
        count = 0;    // initialize to safe default
    }

    public Counter(int startValue) {
        count = startValue;   // overloaded constructor
    }
}

Default vs No-Arg Constructor

If you write no constructor, Java provides a default one that sets numeric fields to 0, booleans to false, and objects to null. The moment you write any constructor, the default disappears.

⚠ Exam Trap: If a class has only a parameterized constructor like public Dog(String name), calling new Dog() is a compile error. Java does NOT automatically provide a no-arg constructor once you define one.

📝 Practice Question 1

A class Timer has only the following constructor: public Timer(int seconds). Which of the following will cause a compile-time error?

📝 Practice Question 2

What is printed by the following code?

public class Box {
    private int size;
        public Box() {
        size = 5;
    }
        public Box(int s) {
        size = s * 2;
    }
        public int getSize() {
        return size;
    }
}
Box b = new Box(3);
System.out.println(b.getSize());
✅ Exam Tip: Constructor questions on the AP exam often involve tracing which overloaded constructor is called based on argument types and count. Match argument count and type to find the correct constructor.

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

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]