π2.3: Methods without Parameters
Table of Contents
π This page is a condensed version of CSAwesome Topic 2.3
π Take notes in a Codespace during class, coding along with the instructor.
- Go to GitHub and click on your picture in the TOP RIGHT corner
- Select
Your repositories
- Open
CS2-Unit-2-Notes
- Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
(unless you already have one listed there), wait for the environment to load, then youβre ready to code!
Calling Methods Without Parameters
Methods are a set of instructions that define behaviors for all objects of a class.
Example: in the
Turtle
class, methods likeforward()
andturnRight()
giveTurtle
objects the ability to move forward and turn 90 degrees right.
To use an objectβs method, you must use the object name and the dot .
operator followed by the method name, for example, yertle.forward();
calls yertle
βs forward
method to move a turtle object forward 100 pixels. These are called object methods or non-static methods. An object method must be called on an object of the class that the method is defined in. Object methods work with the attributes of the object, such as the direction the turtle is heading or its position.
Every method call is followed by parentheses. The parentheses ()
after method names are there in case you need to give the method parameters (data) to do its job, which we will see in the next lesson. You must always include the parentheses after the method name.
π£ object.method();
is used to call an objectβs method!
Procedural Abstraction
Procedural abstraction allows a programmer to use a method and not worry about the details of how it exactly works. For example, we know that if we hit the brakes, the car will stop, and we can still use the brakes even if we donβt really know how they work.
You will learn to write your own methods in Unit 5. In this unit, you should be able to use methods already written for you and figure out what they do.
Click the DevDocs link in the top navigation bar to find the official Java documentation.
When we use methods for a class in a library, we can look up the method signature (or method header), which is the method name followed by a parameter list, in its official documentation.
For example, here is a Student
class with a method signature public void print()
which has an empty parameter list with no parameters. Methods are defined after the instance variables (attributes) and constructors in a class.
VISUALIZATION
This Java visualization shows how a song can be divided up into methods. Click on the next button below the code to step through the code.
Execution in Java always begins in the
main
method in the current class. Then, the flow of control skips from method to method as they are called.
The Songβs print method calls the chorus()
and animal()
methods to help it print out the whole song.
When you call the chorus()
method, it skips to the chorus code, executes and prints out the chorus, and then returns back to the method that called it.
SCOPE: Methods inside the same class can call each other using just methodName()
, but to call non-static methods in another class or from a main method, you must first create an object instance of that class, and then call its methods using the dot operator like this: object.methodName()
.
Remember that if you just declare an object reference without setting it to refer to a new object, the value will be
null
meaning that it doesnβt reference an object. If you call a method on a variable whose value isnull
, you will get aNullPointerException
error, where a pointer is another name for a reference.
π» In-Class Activity
βοΈ Summary
-
Methods are a set of instructions that define the behaviors for all objects of the class.
-
Use dot notation to execute an objectβs method. This is the objectβs name followed by the dot (.) operator followed by the method name and parentheses: object.method();
-
A method signature is the method name followed by the parameter list which gives the type and name for each parameter. Note that methods do not have to take any parameters, but you still need the parentheses after the method name.
-
Procedural abstraction allows a programmer to use a method by knowing in general what it does without knowing what lines of code execute. This is how we can drive a car without knowing how the brakes work.
-
A method or constructor 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 executed or a
return
statement is executed, the flow of control is returned to the point immediately following the method or constructor call. -
A NullPointerException will happen if you try to call an object method on an object variable whose value is
null
. This usually means that you forgot to create the object using thenew
operator followed by the class name and parentheses. -
An object method or non-static method is one that must be called on an object of a class. It usually works with the objectβs attributes.
-
A static method or class method method is one that doesnβt need to be called on an object of a class.
π When class ends, donβt forget to SAVE YOUR WORK!
- Navigate to the
Source Control
menu on the LEFT sidebar - Click the button on the LEFT menu
- Type a brief commit message at the top of the file that opens, for example:
updated Main.java
- Click the small
βοΈ
checkmark in the TOP RIGHT corner - Click the button on the LEFT menu
- Finally you can close your Codespace!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.