0% found this document useful (0 votes)
8 views44 pages

CH 06-1

This document discusses Java methods and their importance in modular programming, emphasizing the divide-and-conquer approach. It covers static methods, method calls, parameter handling, method overloading, and the Java API's role in providing predefined classes. The chapter aims to deepen understanding of method execution, scope, and software reusability in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views44 pages

CH 06-1

This document discusses Java methods and their importance in modular programming, emphasizing the divide-and-conquer approach. It covers static methods, method calls, parameter handling, method overloading, and the Java API's role in providing predefined classes. The chapter aims to deepen understanding of method execution, scope, and software reusability in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

Academic

Academic 2024
2024

Class:
Class: BCS16
BCS16

Chapter
06
Java Methods: A Deeper Look
Java How to Program,
11/e
Abdulaziz Yasin Nageye
Faculty of Computing
© Copyright 2024 by SIMAD University 2
 Best way to develop and maintain a large program is to
construct it from small, simple pieces, or modules. This
technique is called divide and conquer.
 Methods, which we introduced in Chapter 3, help you

modularize programs. In this chapter, we study methods


in more depth.
 You’ll learn more about static methods, which can be

called without the need for an object of the class to exist.


 You’ll also learn how Java is able to keep track of which

method is currently executing, how local variables of


methods are maintained in memory and how a method
knows where to return after it completes execution.

© Copyright 2024 by SIMAD University 3


 You’ve already been working with various program
units in Java. You write programs by combining new
methods and classes with predefined ones available in
the Java Application Programming Interface (also
referred to as the Java API or Java class library) and
in various other class libraries.
 Related classes are typically grouped into packages so

that they can be imported into programs and reused.


 The Java API provides a rich collection of predefined

classes that contain methods for performing common


mathematical calculassions, string manipulations,
character manipulations, input/output operations, error
checking and more.
© Copyright 2024 by SIMAD University 4
Divide and Conquer with Classes and Methods
Classes and methods help you modularize a program by separating
its tasks into self-contained units.
Statements in method bodies
 Written only once
 Hidden from other methods
 Can be reused from several locations in a program
Divide-and-conquer approach
 Constructing programs from small, simple pieces
Software reusability
 Use existing classes and methods as building blocks to create new
programs.
Dividing a program into meaningful methods makes the program
easier to debug and maintain.

© Copyright 2024 by SIMAD University 5


© Copyright 2024 by SIMAD University 6
© Copyright 2024 by SIMAD University 7
Hierarchical Relationship Between Method Calls
Hierarchical form of management (Fig. 6.1).
 A boss (the caller) asks a worker (the called method) to
perform a task and report back (return) the results after
completing the task.
 The boss method does not know how the worker method
performs its designated tasks.
 The worker may also call other worker methods, unbeknown to
the boss.
“Hiding” of implementation details promotes good
software engineering.

© Copyright 2024 by SIMAD University 8


© Copyright 2024 by SIMAD University 9
© Copyright 2024 by SIMAD University 10
 Most methods execute in response to method calls on specific
objects.
 Sometimes a method performs a task that does not depend on an
object. Such a method applies to the class in which it’s declared as a
whole and it’s known as a static method or a class method.

 It’s common for classes to contain convenient static methods


to perform common tasks.
 To declare a method as static, place the keyword static
before the return type in the method’s declaration.
 Calling a static method
 ClassName.methodName(arguments)

© Copyright 2024 by SIMAD University 11


Math Class Methods
Class Math provides a collection of static methods
that enable you to perform common mathematical
calculations.
Method arguments may be constants, variables or
expressions.
Java Math class provides several methods to work on
math calculations like min(), max(), avg(), sin(), cos(),
tan(), round(), ceil(), floor(), abs() etc. In this article, we
will learn about the Java Math class, its basic methods and
constructors provided by Java programming language.

© Copyright 2024 by SIMAD University 12


© Copyright 2024 by SIMAD University 13
© Copyright 2024 by SIMAD University 14
© Copyright 2024 by SIMAD University 15
Why is method main declared static?
The JVM attempts to invoke the main method of the

class you specify—at this point no objects of the class


have been created.
Declaring main as static allows the JVM to invoke

main without creating an instance of the class.


In any Java program, the main() method is the

starting point from where compiler starts program


execution. So, the compiler needs to call the main()
method.

© Copyright 2024 by SIMAD University 16


 Parameters are specified after the method name, inside the
parentheses. You can add as many parameters as you want,
just separate them with a comma.

 There must be one argument in the method call for each


parameter (sometimes called a formal parameter) in the
method declaration.

 Each argument must be consistent with the type of the


corresponding parameter.

© Copyright 2024 by SIMAD University 17


© Copyright 2024 by SIMAD University 18
© Copyright 2024 by SIMAD University 19
© Copyright 2024 by SIMAD University 20
© Copyright 2024 by SIMAD University 21
© Copyright 2024 by SIMAD University 22
Calling Methods

 Three ways to call a method:


 Using a method name by itself to call another method of the
same class
 Using a variable that contains a reference to an object,
followed by a dot (.) and the method name to call a method of
the referenced object
 Using the class name and a dot (.) to call a static method
of a class

© Copyright 2024 by SIMAD University 23


 Non-static methods are typically called instance
methods.
 A static method can call other static methods of

the same class directly and can manipulate static


variables in the same class directly.
 To access the class’s instance variables and instance methods,
a static method must use a reference to an object of the
class.

© Copyright 2024 by SIMAD University 24


Returning from Methods

 There are three ways to return control to the statement that


calls a method.
 If the method does not return a result, control returns when the
program flow reaches the method-ending right brace or when the
statement.
 If the method returns a result, the statement
return;
 When the method evaluates the expression, then returns the result to
the caller
return expression;

© Copyright 2024 by SIMAD University 25


© Copyright 2024 by SIMAD University 26
© Copyright 2024 by SIMAD University 27
© Copyright 2024 by SIMAD University 28
 Java contains many predefined classes that are grouped
into categories of related classes called packages.
 A great strength of Java is the Java API’s thousands of

classes.
 Some key Java API packages that we use in this book

are described in Fig. 6.5.


 Overview of the packages in Java:
 http://docs.oracle.com/javase/7/docs/api/
overview-summary.html
 http://download.java.net/jdk8/docs/api/
overview-summary.html

© Copyright 2024 by SIMAD University 29


© Copyright 2024 by SIMAD University 30
© Copyright 2024 by SIMAD University 31
© Copyright 2024 by SIMAD University 32
© Copyright 2024 by SIMAD University 33
 Declarations introduce names that can be used to refer
to such Java entities.
 The scope of a declaration is the portion of the program

that can refer to the declared entity by its name.


 Such an entity is said to be “in scope” for that portion of the
program.

© Copyright 2024 by SIMAD University 34


 Basic scope rules:
 The scope of a parameter declaration is the body of the method in
which the declaration appears.
 The scope of a local-variable declaration is from the point at which
the declaration appears to the end of that block.
 The scope of a local-variable declaration that appears in the
initialization section of a for statement’s header is the body of the
for statement and the other expressions in the header.
 A method or field’s scope is the entire body of the class.
 Any block may contain variable declarations.
 If a local variable or parameter in a method has the same
name as a field of the class, the field is hidden until the
block terminates execution—this is called shadowing.

© Copyright 2024 by SIMAD University 35


© Copyright 2024 by SIMAD University 36
© Copyright 2024 by SIMAD University 37
© Copyright 2024 by SIMAD University 38
 Method overloading
Method overloading is a concept of Java in which we can create
multiple methods of the same name in the same class, and all
methods work in different ways. When more than one method of the
same name is created in a Class, this type of method is
called Overloaded Methods.
 Compiler selects the appropriate method to call by examining the
number, types and order of the arguments in the call.
 Used to create several methods with the same name that perform the
same or similar tasks, but on different types or different numbers of
arguments.
 Literal integer values are treated as type int, so the method call in line
9 invokes the version of square that specifies an int parameter.
 Literal floating-point values are treated as type double, so the method
call in line 10 invokes the version of square that specifies a double
parameter.

© Copyright 2024 by SIMAD University 39


© Copyright 2024 by SIMAD University 40
© Copyright 2024 by SIMAD University 41
Distinguishing Between Overloaded Methods
The compiler distinguishes overloaded methods by their
signatures—the methods’ name and the number, types and
order of its parameters.
Return types of overloaded methods
 Method calls cannot be distinguished by return type.
Figure 6.10 illustrates generated when two methods have the
same signature and different return types.
Overloaded methods can have different return types if the
methods have different parameter lists.
Overloaded methods need not have the same number of
parameters.

© Copyright 2024 by SIMAD University 42


© Copyright 2024 by SIMAD University 43
END

© Copyright 2024 by SIMAD University 44

You might also like