5.5 Notes on Declaring and Using Methods
Calling Methods
There are three ways to call a method:
Using a method name by itself to call another method of the same class—such as
maximum(number1, number2, number3)
in line 18 of Fig. 5.3.Using a variable’s name, followed by a dot (
.
) and the method name to call a non-static
method of an object—such as the method call in line 13 of Fig. 5.3,input.nextDouble()
, which calls aScanner
method fromMaximumFinder
’smain
method. Non-static
methods are typically called instance methods.Using the class name and a dot (
.
) to call astatic
method of a class—such asMath.sqrt(900.0)
in Section 5.3.
Returning from Methods
There are three ways to return control to the statement that calls a method:
Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.