Back

Activity 4.2 Constructors

divider

Introduction

Activity 4.2

Constructors

Topics

  • What a constructor is and why objects need them
  • No-argument constructors
  • Full-argument constructors
  • Constructor overloading
  • Shadowing problem & the this
  • Constructor parameter validation

What is a Constructor?

A constructor initializes a new object. It sets up initial values for the instance variables and ensures the object begins in a valid state.

It has:

  • the same name as the class
  • no return type (not even void)
  • runs automatically when using new

No-Argument Constructor

A no-arg constructor provides default values when no information is supplied.

class Dog {
Dog() { /* default values */ }
}

Full-Argument Constructor

A full constructor requires all needed info to create a valid object.

But there’s a catch: constructor parameters often shadow instance variables.

Dog(String name) {
this.name = name;
}

The Shadowing Problem

When parameter names match instance variables:

Dog(String name) {
name = name; // WRONG
}

This sets the parameter equal to itself, not the instance variable.

Solution: Use this

this.name = name;

Example: Dog Class With Both Constructors

class Dog {
// Instance variables
String name;
String breed;
int age;
// No-argument constructor
Dog() {
name = "Unknown";
breed = "Unknown";
age = 0;
}
// Full-argument constructor
Dog(String name, String breed, int age) {
this.name = name; // 'this' fixes the shadowing problem
this.breed = breed;
this.age = age;
}
void bark() {
System.out.println(name + " says: Woof!");
}
}

Constructor Overloading

Java allows multiple constructors as long as they have different parameter lists.

This is called constructor overloading (similar to method overloading).

Constructor Validation

Constructors often check whether values are valid before storing them. This pattern appears frequently on AP FRQs.

if (gpa < 0 || gpa > 4.0) gpa = 0.0;

Key Terms

Constructor
A special method that initializes new objects.
No-Argument Constructor
A constructor that provides default values.
Full-Argument Constructor
A constructor that receives values through parameters.
Shadowing
When a parameter name hides an instance variable name.
this
Refers to the current object; fixes the shadowing problem.

'F' → Fullscreen

Objectives

  • icon Write and use constructors to initialize objects.
  • icon Differentiate between no-argument and full-argument constructors.
  • icon Explain and resolve parameter shadowing with this.
  • icon Use constructor overloading to support different initialization needs.
  • icon Apply constructor validation to enforce valid object state.
divider

Activity Tasks

  • icon Create a new project named 4-2-Constructors.
  • icon Complete each task individually.

Task 1: No-Arg and Full Constructors

Step 1: Create a file named Student.java and type the following code:

Student.java
class Student
{
String name;
int gradeLevel;
// No-argument constructor
Student()
{
name = "Unnamed";
gradeLevel = 9;
}
// Full-argument constructor
Student(String name, int gradeLevel)
{
this.name = name;
this.gradeLevel = gradeLevel;
}
void introduce()
{
System.out.println("Hi, I'm " + name + " and I'm in grade " + gradeLevel + ".");
}
}

Step 2: Create a file named Program.java and type the following code:

Main.java
public class Program
{
public static void main(String[] args)
{
Student s1 = new Student(); // Calls no-arg constructor
Student s2 = new Student("Alex", 10); // Calls full constructor
s1.introduce();
s2.introduce();
}
}

Run the program. Notice how each constructor initializes the object differently.


Task 2: Constructor Validation

Modify your Student class to include a gpa instance variable and use a full constructor that enforces valid values.

Student.java
class Student
{
String name;
int gradeLevel;
double gpa;
// No-argument constructor
Student()
{
name = "Unnamed";
gradeLevel = 9;
gpa = 0.0;
}
// Full constructor with validation
Student(String name, int gradeLevel, double gpa)
{
this.name = name;
this.gradeLevel = gradeLevel;
if (gpa < 0.0 || gpa > 4.0)
{
System.out.println("Invalid GPA! Setting to 0.0...");
this.gpa = 0.0;
}
else
{
this.gpa = gpa;
}
}
void showInfo()
{
System.out.println(name + " (Grade " + gradeLevel + ") — GPA: " + gpa);
}
}
Main.java
public class Program
{
public static void main(String[] args)
{
Student s1 = new Student("Alex", 10, 3.8);
Student s2 = new Student("Jordan", 11, 5.2); // invalid GPA triggers fix
s1.showInfo();
s2.showInfo();
}
}

Run the program and confirm that invalid GPA values are corrected. This pattern appears frequently in AP FRQs involving class design.

divider

Sample Output

Your output should look similar to the following:

Sample Output
Hi, I'm Unnamed and I'm in grade 9.
Hi, I'm Alex and I'm in grade 10.
Alex (Grade 10) — GPA: 3.8
Invalid GPA! Setting to 0.0...
Jordan (Grade 11) — GPA: 0.0
divider

Reflection Questions

Write your answers as comments at the bottom of Main.java.

  1. Explain the shadowing problem. Why is the this keyword necessary?
  2. What advantages does constructor overloading give you when designing a class?
divider

Submission

Submit your completed Student.java, Main.java, and reflection responses to the appropriate dropbox.

Activity Complete