Unit 1.13: Creating and Initializing Objects — Constructors¶
Scope: CS Awesome 2, Section 1.13
Learning Goals¶
By the end of this lesson, you should be able to:
- Create an object with
newand a constructor. - Identify constructor signatures.
- Match constructor arguments to parameters.
- Explain constructor overloading and no-argument constructors.
- Trace control flow through object construction.
What a Constructor Does¶
A constructor creates and initializes an object. It has the same name as its class and no return type.
Conceptual class definition:
public class Badge
{
private String label;
public Badge(String initialLabel)
{
label = initialLabel;
}
}
Badge(String) is a constructor, not a method returning a Badge.
Constructor Calls and new¶
Badge visitor = new Badge("Visitor");
| Part | Meaning |
|---|---|
Badge |
reference variable's type |
visitor |
reference variable name |
new |
requests creation of an object |
Badge("Visitor") |
constructor call |
"Visitor" |
constructor argument |
The constructor initializes the new object before control returns to the next statement.
Declaration and construction are different:
Badge visitor; // variable only; no Badge object created
visitor = new Badge("Visitor"); // object created and reference assigned
Constructor Signatures¶
A constructor signature consists of the class name and the ordered parameter types.
public Badge()
public Badge(String label)
public Badge(String label, int level)
Signatures:
Badge()
Badge(String)
Badge(String, int)
A no-argument constructor has an empty parameter list.
Overloaded Constructors¶
Constructors are overloaded when a class provides multiple constructor signatures. They must differ in the number, type, or order of parameters.
Badge a = new Badge();
Badge b = new Badge("Guest");
Badge c = new Badge("Staff", 3);
The compiler selects a constructor from the arguments. These calls are invalid if no matching signature exists:
new Badge(3); // no Badge(int)
new Badge(3, "Staff"); // wrong order
new Badge("Staff", 3, 1); // wrong argument count
Parameters, Arguments, and Call by Value¶
public Badge(String initialLabel, int initialLevel)
initialLabel and initialLevel are parameters. In this call:
new Badge("Staff", 3)
"Staff" and 3 are arguments. Each parameter receives a copy of the corresponding argument value.
Argument order matters because the ordered types identify the constructor.
Object References and null¶
Badge pass = new Badge("Day Pass");
Badge missing = null;
pass refers to a constructed object. missing refers to no object. The reference variable and object are separate entities.
pass ─────> Badge object
missing ──> null
Control Flow During Construction¶
System.out.println("Before");
Badge pass = new Badge("Guest");
System.out.println("After");
At the constructor call:
- memory is allocated for the new object;
- arguments are copied into parameters;
- constructor statements initialize the object;
- the object reference is returned;
- execution continues with the next statement.
Standard-Class Examples¶
String copy = new String("north");
Scanner input = new Scanner(System.in);
These calls use String(String) and Scanner(InputStream) constructors. The API documentation gives the available signatures.
Practice Missions¶
Mission 1: Parse the Construction¶
For this statement, identify the class type, reference variable, constructor signature, and arguments:
Reservation booking = new Reservation("Kim", 4);
Mission 2: Select the Overload¶
An API provides:
Sensor()
Sensor(String)
Sensor(String, double)
For each call, identify the selected constructor or explain why no match exists:
new Sensor();
new Sensor("A7");
new Sensor("A7", 0.25);
new Sensor(0.25, "A7");
new Sensor(7);
Mission 3: Design Constructors¶
For a conceptual Parcel class with destination, mass, and tracking ID, propose:
- a no-argument constructor;
- a constructor that accepts only tracking ID;
- a constructor that accepts all three values.
Write only headers and signatures, then provide one valid call for each.
Mission 4: Reference State¶
Draw the variables and objects after these statements. Count how many objects were created.
String a = new String("east");
String b = a;
String c = new String("east");
String d = null;
Key Summary¶
| Concept | Core idea |
|---|---|
| Constructor | Creates and initializes an object. |
new |
Invokes construction and returns an object reference. |
| Constructor name | Exactly matches the class name. |
| Constructor signature | Class name plus ordered parameter types. |
| No-argument constructor | Constructor with an empty parameter list. |
| Overloading | Multiple constructors with different signatures. |
| Call by value | Parameters receive copies of argument values. |
Source scope: CS Awesome 2, Unit 1.13