2.2 Creating and Storing Objects (Instantiation)
In Java, you can think of everything as a class. If you want to write a program to act as a grade book, GradeBook
is a class. Because of this, you can bundle classes and their functionality. You can even use classes that are written by other people without knowing exactly how they work. In this section, you will explore how to create objects from pre-existing classes.
To better understand how to create an object, let’s first look at the different parts of a class. There are 3 main parts to any class:
Instance Variables
The instance variables, or attributes, hold the state of the object. These are declared at the beginning of the class. When declaring them, they are typically declared as private, which means that they are only accessible within the class.
Instance variables are not usually initialized when you declare them. For primitive variables, that means that these variables are created with default values. When an object has not been created, or “instantiated”, it isn’t pointing to any particular object data. When that happens, the object, in this case, is considered to be a null object. The keyword null
is a special value used to indicate that a reference variable is not associated with any object.
Constructor
The constructor is used to instantiate – create an instance of – an object. Anytime a new object is created, the constructor is run. Notice that the constructor has a different header (first line) compared to other methods. The constructor header has the same name as the class, is always declared public, and has no return type (you will learn more about return type later in this section). A class contains constructors that are invoked to create objects.
Methods
The third part of the class is the methods. These make up the behaviors of the object. There can be any number of methods, some of which take an input, some of which return a value, and some that do either both or neither. You will discuss methods later in this unit.
Let’s take a closer look at the constructor. The first line of the constructor (and other methods) is known as the method signature. A signature consists of the constructor name and the parameter list. In the example below, the signature name is Rectangle
and the parameter list includes rectWidth
and rectHeight
.
The parameter list, in the header of a constructor, lists the types of the values that are passed in and their variable names. These are often referred to as formal parameters. Parameters allow values to be passed to the constructor to establish the initial state of the object.
To create a new rectangle, you call the constructor and pass values that correspond to the parameter list. For example, to create a rectangle with a width of 5 and a height of 3, you would call it from the main
function like this.
In the above example, the 5
and 3
are known as arguments. An argument is a value that is passed into a constructor (or any method) when called. These are often referred to as actual parameters. The actual parameters passed to a constructor must be compatible with the types identified in the formal parameter list.
Arguments are passed to the object using call by value. Call by value initializes the formal parameters with copies of the actual parameters.
As you saw in the example above, every object is created using the keyword new
followed by a call to the class’ constructor. The call to the constructor must contain the list of parameters that match the formal parameters.
Once you create a new object, Java allocates memory for this object. Unlike primitive variables that store the object address, the memory associated with a variable of a reference type holds an object reference value or, if there is no object, null
. This value is the memory address of the referenced object.
To create an object, the arguments need to match the formal parameters, but what if you want to specify a different set of parameters? For example, maybe you want to create a square and only provide one side length.
In Java, you can do this with what is called a constructor overload. Let’s take a look at an example:
Constructors are said to be overloaded when there are multiple constructors with the same name but a different signature. In the example above, you see two constructors with the name Rectangle
. One constructor takes two integers and the other takes one. Since the formal parameter lists are different, the signatures are different, and you can say that the constructor is overloaded. Now you can create a rectangle object by passing either one or two arguments in the call to the class constructor.
You have seen here how you can create the Rectangle
class and instantiate new objects, but you can also use existing classes and class libraries to create objects. When you do this, you need to make use of documentation.
Reading Documentation
Documentation is a great resource to use to see how a class works. As long as you can read the documentation, you don’t need to know how the class works.
Here is an example of the documentation for the String
class:
The documentation for a class tells us how to use the class constructor and various methods that the class provides.
Be sure to check out both files in this example – Rectangle
is the class that rectangle objects are created from, RectangleTester
calls the constructor in Rectangle
to create new objects.
Be sure to check out both files in this example – Point
is the class that point objects are created from, PointTester
calls the constructor in Point
to create new objects.
Be sure to check out both files in this example – Student
is the class that student objects are created from, StudentTester
calls the constructor in Student
to create new objects.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Which of the following choices is a formal parameter of the constructor?
Now you are going to use more instance variables. Here you have a class that represents a cat. Currently, the only instance variable is called breed
. You can make a new Cat
object by passing the breed
to the constructor.
You need to add an instance variable that is a String
called color
, which represents the cat’s color. Add the instance variable and also include it as the second parameter to the constructor.
Test out your Cat
class in CatTester.java.