2.2. Creating and Initializing Objects: Constructors¶
A Java class defines the data (attributes) and behavior (methods) of a set of similar objects. Each class has a special type of method called a constructor that is used to initialize the attributes in a newly created object.
Note
A constructor is a special method that has the same name as the class and is used to initialize attributes of a new object.
A new object is created with the new
keyword followed by the class name.
For example, new World()
calls the World constructor to initialize a new object of the World
class,
which results in the creation of a graphical window used for drawing.
The code new Turtle(world)
calls the Turtle constructor to initialize the
attributes of a new Turtle
object that is placed within the drawing window of the specified world.
// To create a new object and call a constructor write:
// ClassName variableName = new ClassName(parameters);
World world = new World(); // create a new World object
Turtle t = new Turtle(world); // create a new Turtle object
2.2.1. World Class Constructors¶
The World
class has 2 constructors, which means there are two different ways to create a World object.
You can create a World using default values for the size of the graphical window, or you can provide
a specific width and height.
In Java, formal parameters are variables that act as value placeholders that you define for a method. When you place values between the parentheses of a method call, the values get copied into the formal parameter variables. The values placed between the parentheses of a method call are called arguments or actual parameters.
World world1 = new World(); // creates a 640x480 world
World world2 = new World(300,400); // creates a 300x400 world
The first World()
constructor has no parameters (there is nothing between the parentheses) and
initializes a graphical window with a default size of 640x480 pixels.
The second constructor World(int width, int height)
has two formal parameters to initialize the
graphical window to a specific width and height. When you call the second constructor, you must provide actual integer values
for the width and height.
For example, new World(300,400)
creates a world with a graphical window sized 300x400 pixels.
Classes frequently have more
than one constructor, which is called constructor overloading.
There is usually a constructor that has no parameters like the World()
constructor above. This is also
called the no-argument constructor and it sets the
attributes to default values. There can also be constructors with formal parameters to
initialize the attributes, such as the World(int width, int height)
constructor. The World constructor is overloaded
since there are two versions available, each with a different parameter list.
- 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.
2-2-1: Which of these is overloading?
- World w = null;
- This declares a variable w that refers to a World object, but it doesn't create a World object or initialize it.
- World w = new World;
- You must include parentheses () to call a constructor.
- World w = new World();
- Use the new keyword followed by the classname and parentheses to create a new object and call the constructor.
- World w = World();
- You must use the new keyword to create a new object.
2-2-2: Which of these is valid syntax for creating and initializing a World object?
- World w = (300,400);
- This is missing the keyword new and the constructor method name World.
- World w = new World(300,400);
- Use the new keyword followed by the classname and parentheses to create a new object and call the constructor.
- World w = World(300,400);
- This is missing the keyword new.
- World = new World(300,400);
- The lefthand size of the assignment is missing the variable name.
2-2-3: Which of these is valid syntax for creating and initializing a World object with size 300x400?
Note
The turtle world does not use the cartesian coordinate system. The origin (0,0) is in the top left corner, x increases to the right, and y increases as you go down the screen rather than up. This unusual coordinate system is due to historical reasons. When physical devices were first built, they would scan from left to right and then top to bottom.
2.2.2. The Turtle Class Constructors¶
When you use a class that someone has already written for you in a library like the Turtle
class, 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.
A no-arguments constructor will have an empty parameter list.
The Turtle
class has multiple constructors, although it always requires a World as a parameter
in order to have a place to draw. The first constructor places the turtle in a default location in the middle of the world.
The second constructor places the turtle at a certain (x,y) location in the world.
World world = new World();
Turtle t1 = new Turtle(world); //place in center of world
Turtle t2 = new Turtle(50, 100, world); //place at 50, 100
Note
The order of the argument values matter.
The Turtle
constructor takes (x,y,world)
as parameters in that order.
If you mix up the order of the parameters it will cause an error, because the parameters will not be the data types that it expects. This is one reason why programming languages have data types – to allow for error-checking.
- Turtle t = Turtle(world);
- You must use the new keyword to create a new Turtle.
- Turtle t = new Turtle();
- All turtle constructors must take a world as a parameter.
- Turtle t = new Turtle(world);
- This creates a Turtle object and places it in the center of the world.
- World t = new Turtle(world);
- You can not assign a Turtle object to a variable declared to have type World.
2-2-4: Which of these is valid syntax for creating and initializing a Turtle object in the center of the world?
- Turtle t = new Turtle();
- There is no Turtle constructor that takes no parameters according to the figure above.
- Turtle t = new Turtle(50,75);
- There is no Turtle constructor that takes 2 parameters according to the figure above.
- Turtle t = new Turtle(world1);
- This would initialize the Turtle to the middle of the world, not necessarily coordinates (50,150).
- Turtle t = new Turtle(world1,50,75);
- Make sure the order of the parameters match the constructor signature above.
- Turtle t = new Turtle(50,75,world1);
- This matches the second constructor above with the parameters of x, y, and world.
2-2-5: Given the Turtle class and a World object referenced by variable world1, which of the following code segments will correctly create an instance of a Turtle object at (x,y) coordinates (50,75)?
Try changing the code below to create a World
object with 300x400 pixels.
Where is the turtle placed by default? What parameters do you need to
pass to the Turtle
constructor to put the turtle near the top right corner? Recall that (0,0) is
the top left corner and y increases as you go down the window. Experiment with different initial locations for the turtle. What happens if you mix up the order of the parameters?
(If the code below does not work in your browser, you can also copy the code into this repl.it link (refresh page after forking if it gets stuck) or download the files here to use in your own IDE.)
2.2.3. Object Variables and References¶
An object is created when you call a constructor. You need to declare an object variable to
reference the newly created object. An object variable has a type that is a class, rather than a primitive
such as int, double or boolean. Turtle t1
and World world
are both object variable declarations since Turtle and World are classes and not primitive types.
You assign an object variable by calling a constructor on the right hand side of an equal sign, for example World world = new World();
or
Turtle t1 = new Turtle(world);
.
Run the code below, which creates two instances of the Turtle class. Add a third turtle object that draws a square by repeatedly turning left and moving forward. Make sure you give your new turtle a unique variable name.
2.2.4. Formal Parameters and Pass By Value¶
In a later lesson you will learn to write your own classes.
However, if you see a class definition like the one below 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.
- 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.
2-2-8: 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?
When a constructor like Date(2005,9,1)
is called, the formal parameters, (year, month, day), are set
to copies of the actual parameters, which are (2005,9,1). This is pass 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).
- objects
- Objects have attributes and behavior.
- classes
- A class defines the data and behavior for all objects of that type.
- formal parameters
- A formal parameter is in the constructor's signature.
- actual parameters
- A actual parameter (argument) is the value that is passed into the constructor.
2-2-9: Given the constructor signature public World(int width, int height)
, what are width
and height
?
- objects
- Objects have attributes and behavior.
- classes
- A class defines the data and behavior for all objects of that type.
- formal parameters
- A formal parameter is in the constructor's signature.
- actual parameters
- A actual parameter (argument) is the value that is passed into the constructor.
2-2-10: Given the constructor call new World(150, 200)
, what are 150
and 200
?
This lesson introduces a lot of vocabulary, but don’t worry if you don’t understand everything about classes and constructors yet. You will learn more about how this all works in a later lesson when you write your own classes and constructors.
2.2.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.