Section6.7Creating 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.
// 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
Subsection6.7.1Overloading 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.
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.
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();).
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.
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.
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).
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.
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.
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.
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).
Constructors are public and have the same name as the class. Click on the constructor headers which are the first line of the constructors showing their name and parameters.
public class Date {private int year;private int month;private int day;public Date(){ /** Implementation not shown */ }public Date(int year, int month, int day){ /** Implementation not shown */ }public void print(){ /** Implementation not shown */ }}
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?
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);