0% found this document useful (0 votes)
14 views40 pages

Java Methods: Definition and Usage

The document provides an introduction to methods in computer programming, specifically in Java, explaining their purpose in breaking down complex tasks into manageable pieces. It covers method construction, including headers, bodies, and the concept of code reuse, as well as how to call methods from other classes. Additionally, it emphasizes the importance of method modifiers, return types, and the void keyword in method definitions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views40 pages

Java Methods: Definition and Usage

The document provides an introduction to methods in computer programming, specifically in Java, explaining their purpose in breaking down complex tasks into manageable pieces. It covers method construction, including headers, bodies, and the concept of code reuse, as well as how to call methods from other classes. Additionally, it emphasizes the importance of method modifiers, return types, and the void keyword in method definitions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INTRODUCTION

TO METHODS

04/02/2025 ITE 4 – Computer Programming 2


public static void main()

[Link]
[Link]
[Link]
04/02/2025 ITE 4 – Computer Programming 2
A method is a program
module that contains a
series of statements that
carry out a task.
04/02/2025 ITE 4 – Computer Programming 2
1 // This is a simple Java program.
2
3 public class Simple
4{
5 public static void main(String[] args)
6 {
7 [Link]("Programming is great fun!");
8 }
9}
04/02/2025 ITE 4 – Computer Programming 2
1 // This is a simple Java program.
2
3 public class Simple
4{
5 public static void main(String[] args)
6 {
7 [Link]("Programming is great fun!");
8 }
9}
04/02/2025 ITE 4 – Computer Programming 2
Methods can be used to
break a complex
program into small,
manageable pieces.
04/02/2025 ITE 4 – Computer Programming 2
public class BigProblem
{
public static void main(String[] args)
{
Statement;
Statement;
Statement;
Statement;
Statement;
Statement;
}
}

04/02/2025 ITE 4 – Computer Programming 2


public class DividedProblem
{
public static void main(String[] args) {
Statement;
Statement;
Statement; }

public static method2 () {


statement;
statement; }
}

04/02/2025 ITE 4 – Computer Programming 2


04/02/2025 ITE 4 – Computer Programming 2
04/02/2025 ITE 4 – Computer Programming 2
This approach is sometimes
called divide and conquer
because a large problem is
divided into several smaller
problems that are easily solved.
04/02/2025 ITE 4 – Computer Programming 2
This benefit of using methods is
known as code reuse because you
are writing the code to perform a
task once and then reusing it each
time you need to perform the
task.
04/02/2025 ITE 4 – Computer Programming 2
Defining a void
Method

04/02/2025 ITE 4 – Computer Programming 2


04/02/2025 ITE 4 – Computer Programming 2
Understanding
Method Construction

04/02/2025 ITE 4 – Computer Programming 2


A method header—A method’s
header provides information
about how other methods can
interact with it. A method header
is also called a declaration.
04/02/2025 ITE 4 – Computer Programming 2
A method body between a pair
of curly braces—The method
body contains the statements that
carry out the work of the
method. A method’s body is
called its implementation.
04/02/2025 ITE 4 – Computer Programming 2
04/02/2025 ITE 4 – Computer Programming 2
The method header is the first line of a method. It contains
the following:

• Method Modifiers/Access Specifiers


•Return Type
•Method Name/Identifier
•Parenthesis
04/02/2025 ITE 4 – Computer Programming 2
The method header is the first line of a method. It contains
the following:

• Method Modifiers/Access Specifiers


•Return Type
•Method Name/Identifier
•Parenthesis
04/02/2025 ITE 4 – Computer Programming 2
04/02/2025 ITE 4 – Computer Programming 2
The word public means that the
method is publicly available to
code outside the class. The word
static means that the method
belongs to the class, not a
specific object.
04/02/2025 ITE 4 – Computer Programming 2
The method header is the first line of a method. It contains
the following:

• Method Modifiers/Access Specifiers


•Return Type
•Method Name/Identifier
•Parenthesis
04/02/2025 ITE 4 – Computer Programming 2
04/02/2025 ITE 4 – Computer Programming 2
When the key word void
appears here, it means that
the method is a void
method, and does not return
a value.
04/02/2025 ITE 4 – Computer Programming 2
The method header is the first line of a method. It contains
the following:

• Method Modifiers/Access Specifiers


•Return Type
•Method Name/Identifier
•Parenthesis
04/02/2025 ITE 4 – Computer Programming 2
04/02/2025 ITE 4 – Computer Programming 2
In general, the same
rules that apply to
variable names also
apply to method names.
04/02/2025 ITE 4 – Computer Programming 2
The method header is the first line of a method. It contains
the following:

• Method Modifiers/Access Specifiers


•Return Type
•Method Name/Identifier
•Parenthesis
04/02/2025 ITE 4 – Computer Programming 2
04/02/2025 ITE 4 – Computer Programming 2
Quick Assessment

1. A method header is also called an


implementation.
2. When a method is declared with public
access, methods in other classes can call it.
3. Not all methods return a value, but every
method requires a return type.
04/02/2025 ITE 4 – Computer Programming 2
Understanding
Method Calls and
Placement
04/02/2025 ITE 4 – Computer Programming 2
To execute a method, you
invoke or call it. In other
words, a calling method (also
known as a client method)
invokes a called method.
04/02/2025 ITE 4 – Computer Programming 2
public class SimpleMethod
{
public static void main(String[] args)
{
[Link]("Hello from the main method.");
displayMessage();
[Link]("Back in the main method.");
}
public static void displayMessage()
{
[Link]("Hello from the displayMessage method.");
}
}

04/02/2025 ITE 4 – Computer Programming 2


DEMO

04/02/2025 ITE 4 – Computer Programming 2


Quick Assessment

1. Any class can contain an unlimited number


of methods.
2. During one program execution, a method
might be called any number of times.
3. A method is usually written within another
method.
04/02/2025 ITE 4 – Computer Programming 2
Calling Methods
from other Class

04/02/2025 ITE 4 – Computer Programming 2


For us to call a method from another
class we need the Class name and the
method name.

[Link]()

04/02/2025 ITE 4 – Computer Programming 2


DEMO

04/02/2025 ITE 4 – Computer Programming 2


Thank you!

04/02/2025 ITE 4 – Computer Programming 2

You might also like