UOB - Calling a Method (Lesson)

APCompSci_LessonTopBanner.png

Calling a Method

Introduction

An object’s behavior refers to what the object can do or what can be done to it. These behaviors are defined as methods in the class. Consider a Jeep class which will make Jeep objects. What does a jeep do? The Jeep class may have methods to move forward, backward, turn, stop…just to list few.

The cool thing about object oriented programming is that it allows us to use procedural abstractions. Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written. Let’s take a closer look at methods.

Methods

In a class, methods represent the actions of an object. Anything that an object can do will be written as a method. Methods have signatures in the class code. The signature is like the heading. It tells a lot about the method even before we look at the code contained within the method.

A method signature consists of the method name followed by a parameter list.

Similar to our constructors which had signatures with the class name and parameter list 

For example, our Jeep class may have a method for moving forward. The heading could look something like this:

public void forward()

The word public is a keyword which provides access to the method. That means that this method can be accessed outside the class. Any programmer who wants to create a Jeep object can make the jeep move forward using this method.

All of the methods that we write in AP Computer Science will be public. 
 

The next keyword is void. The keyword void means that nothing will be returned when this method is executed. The method has an action but no data will be returned to the program as a result of moving the jeep forward. Anytime you see void in a heading it means that no data will need to be stored in the code calling the method.

The name forward is a simply the method name. This can be anything. It is a good idea to come up with a name that represents what the method does as this makes it easier for programmers to read and understand the code.

Some methods will have parameters and others will have an empty parameter list.

public void forward(int distance)

Consider this forward method. It passes an int representing the distance. If this method is called, the programmer can decide how far forward the jeep should move. Notice, this is an example of an overloaded method since it has the same name as the other forward method but a different parameter list.

I see that signatures (headings) do not have a semi-colon at the end. Only statements have semi-colons. Flow of Control

To use a method we have to call it on an object. When a method is called on an object we use what is called the dot operator. Take the name of the object followed by a dot and the name of the method. For example, if we create a jeep object called myTruck. We can then call the forward method to make it move forward.

Jeep myTruck = new Jeep();

myTruck.forward();

The flow of a control of a program is the order that the code is executed when the code is run. Consider this sample program:

public class Tester
{
public static void main (String args [])
{
System.out.pring1n("Hello World");
int num = 6;
System.out.print1n("I drove my jeep " + num + " miles today.");
}
} 

This is a simple program where each line of code is executed in order. The statements are executed sequentially. First “Hello World” will be printed. Next, the variable num will be created. Then the remaining output statement will be printed.

When a program contains a method call, the flow of control is interrupted while the method is executed.

public class Tester
{
public static void main (String args [])
{
System.out.print1n("Hello World");
int num = 6;
Jeep myTruck = new Jeep();
myTruck.forward(6);
System.out.print1n("I drove my jeep " + num + " miles today.");
}
} Now, "Hello World" will be printed, num and myTruck will be created, then the forward method will be executed. When that happens the program jumps to the Jeep class to execute all the statements that are in the forward method body. After that is done, the program comes back to the Tester class and prints the next statement.

Method Returns

A method that uses the keyword void in the signature does not have a return value that needs to be used in an expression. It is an action that will simply be executed. However, there are some methods which will have a return. Consider these two method signatures from the Jeep class:

 

 

public void turn()

public String horn()

The turn method will allow the jeep to turn and change direction. No return value is necessary so it is a void method. The horn method does not have the keyword void but instead it has a data type, in this case String. That tells us that the horn method will return a String. Perhaps it will say “Beep!”.

All methods will either have the word void or a data type in the signature. When a data type is in the signature a value of that type will be returned to the program when this method is called (executed). There needs to be a place to store or use that data in the program. If we call the horn method we will get a String returned so we need to have a place to store or use it:

public class Tester
{
public static void main (String args [])
{
System.out.print1n("Hello World");
int num = 6;
Jeep myTruck = new Jeep () ;
myTruck.forward(6);
String sound = myTruck.horn() ;
System.out.print1n("I drove my jeep " + num + " miles today.");
System.out.print1n (sound);
}
} 

Notice that the horn method was called and the data that was returned is stored in an new String, sound.

Any data that is returned from a method must be stored in a variable with the matching data type, or used in an expression.

If the dot operator is used on a null reference, an error will occur.

Exclamation point  

public class Tester
{
public static void main (String args [])
{
System.out.print1n ("Hello World");
int num = 6;
Jeep my Truck; (This object has been declared but not instianted!)
myTruck.forward(6);
String sound = myTruck.horn();

System.out.print1n("I drove my jeep " + num + "miles today.");
System.out.print1n(sound);
}
} 

The myTruck object will have a null reference now since it was not actually created. When the forward method is called in the next statement a NullPointerException will be thrown which is an error message letting you know that the object is null.

BookIcon.png 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..

Complete the Review at the end of each chapter.

READ Chapter 42: More about Objects and Classes

READ Chapter 43: Method Parameters

Parameters and Objects Practice

Practice Icon Practice-It! Self-Check

  • Go to the PracticeIt website Links to an external site..
  • Create an account.
  • Click on Start Practicing!
  • Go to the most recent edition.
  • Click on Chapter 3: Parameters and Objects.
  • Complete Self-Checks: 3.2 – 3.3 & 3.6 – 3.9

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS