UOB - Creating and Storing Objects (Instantiation) (Lesson)
Creating and Storing Objects (Instantiation)
Introduction
View the video below.
Object Data Type
Let’s take a look at how to create an object data type. Recall that in order to create an int data type we first need to declare the type, give it an identifier, then set it to a value.
For example,
int age = 17;
First, the data type int is declared. Then an identifier (name) is given. Next comes the assignment operator followed by the data. We will use the same pattern when creating a data type that is an object (not primitive).
For example,
Robot bill = new Robot();
There are a few differences when creating an object versus primitive data. Let’s look at what is the same first.
Both examples state what type of data we are declaring (int or Robot). Both examples give the variable a name (age, bill). Both examples use the assignment operator (=).
So what are the differences?
17 and new Robot()
A New Keyword
With primitive data we simply state the data that will be used. When creating objects, we must use the new keyword. This literally means that a new Robot object is being created. Following new Robot there are parenthesis (). When an object is created, or instantiated, parenthesis are used to pass along any information (attributes) needed to create the object.
Robot fred = new Robot(2);
Robot fred has the number 2 in parenthesis. That means the int 2 was passed to the constructor when this robot was created. The int represents data that was needed to fill an attribute. The passed data is called a parameter. Perhaps the number of arms a robot has is an attribute and fred will be a robot with 2 arms.
Does that mean that our robot bill has no arms? Not necessarily. When the parenthesis are empty, no parameters, the default constructor is used. The attributes (instance variables) for the object were filled with default values.
Recall that it is possible to declare a data type but not initialize it:
int age;
The value for age may be initialized somewhere later in the program. The same can happen when we create an object:
Robot fred;
With this statement we have declared that there will be a Robot named fred but we have not created it yet. In the computer memory fred is set to null. The keyword null is a special value used to indicate that a reference is not associated with an object…yet.
Let’s create Car objects that have two parameters: manufacturer and year.
Car myCar = newCar("Toyota", 2019);
Car dadsCar = new Car("Kia", 2015);
The two parameters that are listed in the parenthesis are a String and an int. The String represents the car manufacturer and the int represents the year the car was made.
If we create a Car without parameters then the manufacturer and year will be filled by whatever values are programmed into the default constructors. We will not know what those are until we look at the class code or access them through a method (more on that later).
Car momsCar = new Car();
It looks like mom got a new Car but we don’t know what it is yet!
Constructors
Later in the course we will learn all the details of creating a class, that blue print for objects. For now, we will focus on the constructors used to create objects.
We know our Car objects can take two parameters. In the Car class, there are constructors that take two parameters. The default constructor will not take any parameter since it will use default values. The other constructor, we will call the initializing constructor, will take two parameters that show the types of variables needed to create a Car.
public Car ()
public Car (String make, int year)
The signature for the constructors show the constructor (class) name and the parameter list. The parameter list, in the heading of the constructor, lists the types of values that are passed and their variable names. These are often referred to as formal parameters.
When we create the Car object the values that are passed to the constructor, for example “Toyota” and 2019 in myCar, are referred to as actual parameters. Think of it as make and year in the signature are the identifiers so they are formal parameters. “Toyota” and 2019 are the data values so those are actual parameters. The actual parameters must match the formal parameters so don’t list the year first and then the manufacturer! The Car takes a String and then an int and any new Car object must list the parameters in that order, unless it uses the default constructor.
The Car class has two constructors, default, and initializing, and they both have the same names but different parameters lists in their signatures. This is an example of overload methods. Overloaded methods are methods, in this case the constructors, that have the same name but different parameter list in the signature.
Click on "Introduction to Computer Science Using Java" below to open the required reading that is listed.
Introduction to Computer Science Using Java Links to an external site.
READ: Chapter 41: Object References
Constructors and Signatures
Click below to begin the Default or Initializing Constructor Activity. Drag and drop the orange rectangle onto the correct category rectangle.
Constructor Signature Matching Activity
Click below to begin the Constructor Signature Matching Activity.
IMAGES CREATED BY GAVS