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

Lecture 8

The document explains Java methods, which are blocks of code that perform actions when called and can accept parameters. It covers method declaration, calling methods, passing arguments, return values, method overloading, and variable scope. Additionally, it provides examples and emphasizes the importance of reusing code through methods.

Uploaded by

amirlanalmasov25
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)
8 views21 pages

Lecture 8

The document explains Java methods, which are blocks of code that perform actions when called and can accept parameters. It covers method declaration, calling methods, passing arguments, return values, method overloading, and variable scope. Additionally, it provides examples and emphasizes the importance of reusing code through methods.

Uploaded by

amirlanalmasov25
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
You are on page 1/ 21

Methods

Java Methods

A method is a block of code which only runs when it is called.

You can pass data, known as parameters, into a method.

Methods are used to perform certain actions, and they are also known as functions.

Why use methods? To reuse code: define the code once, and use it many times.

2
Java Methods

3
Java Methods

A method must be declared within a class. It is defined with the name of the method, followed by
parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can also
write your own methods to perform certain actions:

4
Call a Method

To execute a method, it should be called. To call a method in Java, write the method's name followed by
two parentheses () and a semicolon;

In the following example, myMethod() is used to print a text (the action), when it is called:

5
Call a Method

A method can also be called multiple times:

6
Task

7
Parameters and Arguments
Information can be passed to methods as a parameter. Parameters act as variables inside the method.
Parameters are specified after the method name, inside the parentheses. You can add several
parameters, just separate them with a comma.
The following example has a method that takes a String called fname as parameter. When the method
is called, we pass along a first name, which is used inside the method to print the full name:

8
Attention

When a parameter is passed to the method, it is called an argument. So, from the example above:
fname is a parameter, while Liam, Jenny and Anja are arguments.

9
Multiple Parameters
In Java methods can have multiple parameters.

Note that when you are working with multiple parameters, the method call must have the same number of
arguments as there are parameters, and the arguments must be passed in the same order.

10
A Method with If...Else

It is common to use if...else statements inside methods:

11
Return Values

We used the void keyword in all examples, which indicates that the method should not return a value.

If you want the method to return a value, you can use a primitive data type (such as int, char, etc.)
instead of void, and use the return keyword inside the method:

12
Another example

This example returns the sum of a method's two parameters:

13
Another example

You can also store the result in a variable (recommended, as it is easier to read and maintain):

14
Method Overloading

With method overloading, multiple methods can have the same name with different parameters:

15
Example

Instead of defining two methods that should do the same thing, it is better to overload one.

In the example below, we overload the plusMethod method to work for both int and double:

16
Example(continuation)

Note: Multiple methods can have the same name as long as the number and/or type of parameters are
different.

17
Java Scope

In Java, variables are only accessible inside the region they are declared. This is called scope. Variables
declared directly inside a method are available anywhere in the method following the line of code in
which they were declared:

18
Block Scope

A block of code refers to all of the code between curly braces {}.

Variables declared inside blocks of code are only accessible by the code between the curly braces,
which follows the line in which the variable was declared:

19
Attention

A block of code may exist on its own or it can belong to an if, while or for statement. In the case of for
statements, variables declared in the statement itself are also available inside the block's scope.

20
Used links

1)https://www.w3schools.com/java/java_methods.asp
2)https://www.geeksforgeeks.org/methods-in-java/

21

You might also like