콘텐츠로 이동

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:

  1. Create an object with new and a constructor.
  2. Identify constructor signatures.
  3. Match constructor arguments to parameters.
  4. Explain constructor overloading and no-argument constructors.
  5. 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:

  1. memory is allocated for the new object;
  2. arguments are copied into parameters;
  3. constructor statements initialize the object;
  4. the object reference is returned;
  5. 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:

  1. a no-argument constructor;
  2. a constructor that accepts only tracking ID;
  3. 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