Skip to main content

Section 6.7 Creating and Initializing Objects: Constructors

A Java class defines what objects of the class know (attributes) and what they can do (behaviors). Each class has constructors like Player() and Person("Grace Hopper") which are used to initialize the attributes in a newly created object.
For example, here is how we create a Person object using their name, email and phone number.
// To create a new object, write:
// ClassName variableName = new ConstructorName(arguments);
Person p = new Person("Pat","pat@gmail.com","123-456-7890");
A new object is created with the new keyword followed by the class name (new Class()). When this code executes, it creates a new object of the specified class and calls a constructor, which has the same name as the class. For example, new Date() creates and initializes a new object of the Date class.
// To create a new object and call a constructor write:
// ClassName variableName = new ClassName(parameters);
Monster monster = new Monster(); // create a new Monster object
String name = "Ada Lovelance";
Student aStudent = new Student(name); // create a new Student object

Subsection 6.7.1 Overloading Constructors

There can be more than one constructor defined in a class. This is called overloading the constructor. There is usually a constructor that has no parameters (nothing inside the parentheses following the name of the constructor) like the Monster() constructor above. This is also called the no-argument constructor. The no-argument constructor usually sets the attributes of the object to default values.
There can also be other constructors that take parameters like the Student(name) constructor call above. A parameter (also called actual parameter or argument) is a value that is passed into a constructor. It can be used to initialize the attribute of an object.

Subsection 6.7.2 Object Variables and References

You can also declare an object variable and initialize it to null (Monster m1 = null;). An object variable holds a reference to an object. A reference is a way to find the object in memory. It is like a tracking number that you can use to track the location of a package.
Watch the video below about null.
The code Monster m1 = null; creates a variable m1 that refers to a Monster object, but the null means that it doesn’t refer to an object yet. You could later create the object and set the object variable to refer to that new object (m1 = new Monster()). Or more commonly, you can declare an object variable and initialize it in the same line of code (Monster m2 = new Monster();).

Subsection 6.7.3 Constructor Signatures

When you use a class that someone has already written for you in a library that you can import you can look up how to use the constructors and methods in the documentation for that class. The documentation will list the signatures (or headers) of the constructors or methods which will tell you their name and parameter list. The parameter list, in the header of a constructor, lists the formal parameters, declaring the variables that will be passed in as values and their data types.
Constructors are overloaded when there are multiple constructors, but the constructors have different signatures. They can differ in the number, type, and/or order of parameters.
This is an example of a class definition for a class called Date, you should be able to pick out the attributes (instance variables) and the constructors and know how to use them.
Figure 6.7.1. Figure 4: A Date class showing attributes and constructors

Subsection 6.7.4 Formal and Actual Parameters

When a constructor like Date(2005,9,1) is called, the formal parameters, (year, month, day), are set to copies of the actual parameters (or arguments), which are (2005,9,1). This is call by value which means that copies of the actual parameter values are passed to the constructor. These values are used to initialize the object’s attributes.
Figure 6.7.2. Figure 5: Parameter Mapping
The type of the values being passed in as arguments have to match the type of the formal parameter variables. We cannot give a constructor a String object when it is expecting an int. The order of the arguments also matters. If you mix up the month and the day in the Date constructor, you will get a completely different date, for example January 9th (1/9) instead of Sept. 1st (9/1).

Subsection 6.7.5 Summary

  • Constructors initialize the attributes in newly created objects. They have the same name as the class.
  • A constructor signature is the constructor name followed by the parameter list which is a list of the types of the parameters and the variable names used to refer to them in the constructor.
  • Overloading is when there is more than one constructor. They must differ in the number, type, or order of parameters.
  • New is a keyword that is used to create a new object of a class. The syntax is new ClassName(). It creates a new object of the specified class and calls a constructor.
  • A no-argument constructor is a constructor that doesn’t take any passed in values (arguments).
  • Parameters allow values to be passed to the constructor to initialize the newly created object’s attributes.
  • The parameter list, in the header of a constructor, is a list of the type of the value being passed and a variable name. These variables are called the formal parameters.
  • Actual parameters are the values being passed to a constructor. The formal parameters are set to a copy of the value of the actual parameters.
  • Formal parameters are the specification of the parameters in the constructor header. In Java this is a list of the type and name for each parameter (World(int width, int height).
  • Call by value means that when you pass a value to a constructor or method it passes a copy of the value.

Subsection 6.7.6 Practice

Checkpoint 6.7.3.

Which of these is overloading?
  • When a constructor takes one parameter.
  • For a constructor to be overloaded there must be more than one constructor.
  • When a constructor takes more than one parameter.
  • For a constructor to be overloaded there must be more than one constructor.
  • When one constructor is defined in a class.
  • For a constructor to be overloaded there must be more than one constructor.
  • When more than one constructor is defined in a class.
  • Overloading means that there is more than one constructor. The parameter lists must differ in either number, order, or type of parameters.

Checkpoint 6.7.4.

Checkpoint 6.7.5.

Given the Date class in the figure above and assuming that months in the Date class are numbered starting at 1, which of the following code segments will create a Date object for the date September 20, 2020 using the correct constructor?
  • Date d = new Date();
  • This would initialize the date attributes to today’s date according to the constructor comment above, which might not be Sept. 20, 2020.
  • Date d = new Date(9,20);
  • There is no Date constructor that takes 2 parameters according to the figure above.
  • Date d = new Date(9,20,2020);
  • The comment for the second constructor in the Date class above says that the first parameter must be the year.
  • Date d = new Date(2020,9,20);
  • This matches the second constructor above with the parameters year, month, day.
  • Date d = new Date(2020,20,9);
  • Make sure the order of the parameters match the constructor signature above.

Checkpoint 6.7.6.

Consider the following class. Which of the following successfully creates a new Cat object?
public class Cat
{
private String color;
private String breed;
private boolean isHungry;

public Cat()
{
color = "unknown";
breed = "unknown";
isHungry = false;
}

public Cat(String c, String b, boolean h)
{
color = c;
breed = b;
isHungry = h;
}
}

I. Cat a = new Cat();
II. Cat b = new Cat("Shorthair", true);
III. String color = "orange";
boolean hungry = false;
Cat c = new Cat(color, "Tabby", hungry);
  • I only
  • I is one of the correct constructors but the second constructor can also be used.
  • I and II
  • II is not correct because there is no Cat constructor that takes 2 parameters.
  • I and III
  • I and III call the correct constructors.
  • I, II, and III
  • II is not correct because there is no Cat constructor that takes 2 parameters.
  • II and III
  • II is not correct because there is no Cat constructor that takes 2 parameters.

Checkpoint 6.7.7.

Consider the following class. Which of the following code segments will construct a Movie object m with a title of “Lion King” and rating of 8.0?
public class Movie
{
private String title;
private String director;
private double rating;
private boolean inTheaters;

public Movie(String t, String d, double r)
{
title = t;
director = d;
rating = r;
inTheaters = false;
}

public Movie(String t)
{
title = t;
director = "unknown";
rating = 0.0;
inTheaters = false;
}
}
  • Movie m = new Movie(8.0, "Lion King");
  • There is no Movie constructor with 2 parameters.
  • Movie m = Movie("Lion King", 8.0);
  • There is no Movie constructor with 2 parameters.
  • Movie m = new Movie();
  • This creates a Movie object but it does not have the correct title and rating.
  • Movie m = new Movie("Lion King", "Disney", 8.0);
  • This creates a Movie object with the correct title and rating.
  • Movie m = new Movie("Lion King");
  • This creates a Movie object but it does not have a rating of 8.0.
You have attempted of activities on this page.