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.


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.

Two World constructors

Figure 1: Two World constructors

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.

exercise Check your understanding


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.


Figure 2: The coordinate (0,0) is at the top left of the Turtle world.

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.

Turtle Class Constructor Signatures and Parameters

Figure 3: Turtle Class Constructor Signatures and Parameters

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


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.

exercise Check your understanding

coding exercise Coding Exercise:

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);.

coding exercise Coding Exercise:

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.

A Date class showing attributes and constructors

Figure 4: A Date class showing attributes and constructors

exercise Check your understanding

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.

Parameter Mapping

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).

exercise Check your understanding

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.

You have attempted of activities on this page