UNIT 1

Object Creation and Storage

Topic 1.13: Instantiation

AP Computer Science A

Using Objects and Methods

Learning Objectives

1.13.A

Identify, using its signature, the correct constructor being called.

1.13.B

Develop code to declare variables of the correct types to hold object references.

1.13.C

Develop code to create an object by calling a constructor.

What is a Constructor?

Definition: A constructor is a special method used to create and initialize objects.

Key Characteristics:

  • Has the same name as the class
  • Called when an object is created using the new keyword
  • No return type (not even void)
  • Used to set initial values for object attributes
public class Student { // Constructor public Student(String name, int grade) { // initialization code } }

Constructor Signature

A constructor signature consists of:

  1. The constructor's name (same as class name)
  2. The ordered list of parameter types
public class Car { // Signature: Car(String, int, double) public Car(String make, int year, double price) { // constructor body } }
Note: The parameter list specifies both the types of values passed and their variable names.

Constructor Overloading

Constructors are overloaded when there are multiple constructors with different signatures.

public class Book { // Constructor 1: Book(String) public Book(String title) { // initialize with title only } // Constructor 2: Book(String, String) public Book(String title, String author) { // initialize with title and author } // Constructor 3: Book(String, String, int) public Book(String title, String author, int pages) { // initialize with all three } }

Object References and Variables

A variable of a reference type holds an object reference or, if there is no object, null.

Declaring Reference Variables:

// Declare a variable to hold a Student reference Student student1; // The variable exists but holds null // (no object has been created yet) // Declare and initialize to null explicitly String name = null;
Important: Declaring a reference variable does NOT create an object. It only creates a variable that can refer to an object.

Creating Objects with new

An object is created using the new keyword followed by a call to one of the class's constructors.

Syntax:

ClassName variableName = new ConstructorName(arguments);

Examples:

// Create a String object String greeting = new String("Hello"); // Create a Student object Student alice = new Student("Alice", 11); // Create an ArrayList object ArrayList numbers = new ArrayList();

Constructor Parameters

Parameters allow constructors to accept values to establish the initial values of the attributes of the object.

Example: Rectangle Class

public class Rectangle { private double width; private double height; // Parameters: w and h public Rectangle(double w, double h) { width = w; // Initialize width height = h; // Initialize height } }

Constructor Arguments

A constructor argument is a value that is passed into a constructor when the constructor is called.

Requirements:

  • Arguments must be compatible in order and number with the parameter list
  • Arguments are passed using call by value
  • Call by value initializes parameters with copies of the arguments
// Constructor with parameters (w, h) Rectangle rect = new Rectangle(10.5, 7.3); // ^^^^ ^^^ // arguments passed // 10.5 is copied to parameter w // 7.3 is copied to parameter h

Key Takeaways

Constructors

  • Have the same name as the class
  • Can be overloaded with different signatures
  • Initialize object attributes

Object Creation

  • Use the new keyword
  • Reference variables hold object references or null
  • Arguments are passed by value

Execution

  • Constructor calls interrupt sequential flow
  • Control returns after constructor completes
1 / ?