The existing Body class

6.3 The existing Body class

Exercise 6.10 Open the source code of the Body class and examine it.

Looking at the source code of the Body class, two aspects are worth discussing a bit further. The first is the fact that the class has two constructors (Code 6.1). This is another example of overloading: It is perfectly legal to have two constructors in a class if they have different parameter lists.

In our case, one constructor has no parameters at all, and the other has four parameters.

Code 6.1 Constructors of class

public class Body extends SmoothMover Body

// some code omitted private double mass; /**

* Construct a Body with default size, mass, movement and color. */

public Body() {

this (20, 300, new Vector(90, 1.0), defaultColor);

* Construct a Body with a specified size, mass, movement and * color. */

public Body( int size, double mass, Vector movement, Color color) {

this .mass = mass; addForce(movement); GreenfootImage image = new GreenfootImage (size, size); image.setColor (color); image.fillOval (0, 0, size-1, size-1); setImage (image);

} // more code omitted

Terminology A constructor without any parameters is also called a default constructor.

84 Chapter 6 | ■ Interacting objects: Newton’s Lab

The default constructor makes it easy for us to create bodies interactively without having to specify all the details. The second constructor allows creation of a body with custom size, mass, movement, and color. This constructor is used, for example, in the Space class to create the Sun, planet, and Moon.

The second constructor initializes the state of the actor using all its parameter values that have been passed in. The first constructor looks more mysterious. It has only one line of code:

this (20, 300, new Vector(90, 1.0), defaultColor);

Concept:

The line looks almost like a method call, except that it uses the keyword this instead of a method name. Using this call, the constructor executes the other constructor (the one with the

The keyword this is used to call one

four parameters), and provides default parameters for all the four values. Using the this

constructor from

keyword in this way (like a method name) is only possible within constructors to call another

another, or to refer

constructor as part of the initialization.

to the current object.

There is a second use of the this keyword:

this .mass = mass;

Here we have another example of overloading: The same name is used for two variables (a param- eter and an instance field). When we assign these values, we need to specify which of these two variables named mass we mean on each side of the assignment.

When we write mass without any qualification, then the closest definition of a variable with that name is used—in this case, the parameter. When we write this.mass , we specify that we mean the mass field of the current object. Thus, this line of code assigns the parameter named mass to the field named mass .

Exercise 6.11 Remove the “ this.” segment before the mass in the line of code shown above, so that it reads

mass = mass; Does this code compile? Does it execute? What do you think this code does? What is its

effect? (Create an object and use its Inspect function to examine the mass field. Once finished experimenting, restore the code to how it was before.)

The second aspect that is worth exploring a little further is the two lines near the top of the class, shown in Code 6.2.

Code 6.2 Declaration of

private static final double GRAVITY = 5.8; constants

private static final Color defaultColor = new Color(255, 216, 0);

These two declarations look similar to field declarations, except that they have the two keywords static final inserted after the keyword private .

6.4 First extension: Creating movement

Concept:

This is what we call a constant. A constant has similarities to a field, in that we can use the name in our code to refer to its value, but the value can never change (it is constant). It is the final

A constant is a named value that

keyword that makes these declaration constants.

can be used in

The effect of the static keyword is that this constant is shared between all actors of this class,

similar ways as a variable, but can

and we don’t need separate copies of it in every object. We encountered the static keyword

never change.

before (in Chapter 3), in the context of class methods. Just as static methods belong to the class itself (but can be called from objects of that class), static fields belong to the class and can be accessed from its instances.

In this case, the constants declared are a value for gravity 1 (to be used later), and a default color for the bodies. This is an object of type Color , which we will discuss in more detail below.

It is good practice to declare fields constant that should not change in a program. Making the field constant will prevent accidental change of the value in the code.