2.3 Calling a Void Method
As stated earlier, objects have both state and behavior. The state of the object is stored in the instance variables and can be initialized through the constructor. An object’s behavior refers to what the object can do (or what can be done to it) and is defined by methods. Methods are procedures that allow us to control and define the behavior of an object.
In this section, you are going to look at methods that do not take any input and do not return any values. In subsequent sections, you will take a look at methods that take an input and/or return a value.
As you saw with constructors, methods have a signature that consists of the name of the method and the parameter list. A method signature for a method without parameters consists of the method name and an empty parameter list. Here are some examples:
Each method signature has four parts:
public - The public statement tells Java which classes can see and use the method. In this course, you will see public methods that allow other classes to call the methods and private methods which are only available to that particular class.
void - After the public or private declaration, the signature contains the return type. You will explore return value in a subsequent section, but when a method doesn’t return a value, you use void
as the return type. Some return type or void
is required for all methods, but not for the constructor.
methodName - After the return type, you see the name of the method. While the name can be anything, it is important to give methods names that make sense. For example, if you want a method to print the value of a rectangle width, you may name it printWidth()
so that the user would know what to expect from the method.
() - Finally you have a set of parentheses that hold the parameter list. In this case, you have no parameters, so you include an empty set of parentheses.
Methods allow programmers to perform a specific task without having to know the details of how that method was written. This is known as procedural abstraction. The programmer only needs to know what the method does, including any inputs and any outputs to the method.
When calling a method (or constructor), the call interrupts the sequential execution of statements, causing the program to first execute the statements in the method or constructor before continuing. Once the last statement in the method or constructor has been executed or a return statement is executed, the flow of control is returned to the point immediately following where the method or constructor was called.
Notice in this example how the program jumps from the main
method in line 6 to the printHello
method on line 10. After completing the printHello
method, the program resumes in the main function with line 7.
Methods can be called from any other method, including the main method or another method in a class. How a method is called depends on the type of method that you are calling. In this section, you are going to look at calling non-static void methods. Non-static methods are called through objects of a class and you need to create an object before you can call them. In contrast, static methods can be called without creating a particular instance of an object.
User input is an important part of any user program. While this course will not go into a lot of detail on user input, it is important to understand the basics to help make interactive programs.
User input in this course will be accomplished using a scanner object. A scanner object is a type of variable that allows you to read from the console.
Here is the basic setup for using the scanner.
Notice that the program imports the Scanner from the java.util
library. Java uses import statements to load additional libraries that are not available by default.
Once imported, you create an input
variable using the Scanner
class. In this case, the scanner is reading from System.in
which is the console.
After creating the scanner object, you can then use the variable to read different values in from the user. Depending on the value type, you use will use a different method to import the value. See the table below:
Type | Command |
---|---|
String | input.nextLine() |
int | input.nextInt() |
double | input.nextDouble() |
Let’s take a look at an example with the Rectangle
class below.
Notice in this example how the two objects, r1
, and r2
are created using the Rectangle
constructor. To call the printArea()
function, the dot operator is used along with the object name.
It is important to note that an object must be instantiated to call a method. If the object is only declared, any attempt to call a method on that object will result in a NullPointerException
being thrown. For example:
Since a void method doesn’t have a return type, it must be called as a separate call, not as part of an expression. As you saw in the example above, the printArea()
method was called on a separate line. Calling it to assign a value or as part of a print statement will cause an error.
This program has print statements to mark the order of execution of the program.
Look at the output and find where the statement was printed in the code. That will show you how execution jumps from main
to Executable
and back as methods are called.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Which of the following is a correctly written method for the class below?
You and a friend are going out for their birthday. You have decided to treat your friend, so you’re paying for the activity. However, since you have a fixed amount of money to spend on fun things, you need to track how much the outing will cost so you can update your budget.
Write a program to help yourself estimate what the total cost of the activity will be. Your program will estimate the cost by taking the cost of the activities for one person and estimating how much it will cost for two people.
Here’s what you know about your activities:
- Brunch - you know you typically get cheap entrees, so you expect that your friend’s entree will be twice as expensive as yours
- Movies - since a movie ticket is charged per person, you and your friend will cost the same
- Cake - you like the triple-layer slices, but your friend likes cupcakes. Your friend’s cake will cost 1/3 as much as yours.
Your program should ask how much YOUR brunch cost, how much a movie ticket costs per person, and how much YOUR cake costs. It should then compute how much your friend’s costs will be based on the information above. Be sure your program takes the input in this exact order.
Then print how much brunch will cost (for both of you), how much movie tickets will cost (for both of you), and how much the cake will cost (for both of you). Then print the grand total for the day.
Your output should look something like this: