SlideShare a Scribd company logo
7
Most read
10
Most read
11
Most read
WORKING WITH
METHODS IN JAVA
Computer Programming in Java
At the end of this session, you are expected to:
◦Identify the different parts of a method in Java
◦Identify the different kinds of method
◦Create Java 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.
Create a Method
◦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 create your own
methods to perform certain actions:
Create a Method (example)
public class MyClass {
static void myMethod() {
// code to be executed
}
}
Example Explained
◦ myMethod() is the name of the method
◦ static means that the method belongs to the MyClass class and not
an object of the MyClass class. You will learn more about objects
and how to access methods through objects later in this tutorial.
◦ void means that this method does not have a return value. You will
learn more about return values later in this chapter
Call a Method
◦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:
Call a Method (Example)
public class MyClass {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}
// Outputs "I just got executed!"
Call a Method (Example)
public class MyClass {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod(); //method call
myMethod(); //method call
myMethod(); //method call
}
}
Java Method Parameters
◦ Parameters and Arguments
◦ Information can be passed to methods as parameter. Parameters act
as variables inside the method.
◦ 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.
Java Method Parameters (Example)
public class MyClass {
static void myMethod(String fname) {
System.out.println(fname + " Refsnes");
}
public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
myMethod("Anja");
}
}
OUTPUT:
Liam Refsnes
Jenny Refsnes
Anja Refsnes
The 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:
Argument
◦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
Multiple Parameters
public class MyClass {
static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
}
public static void main(String[] args) {
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
}
}
OUTPUT
Liam is 5
Jenny is 8
Anja is 31
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.
Return Values
◦The void keyword, used in the examples above, 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:
Example 1 (Single Parameter)
public class MyClass {
static int myMethod(int x) {
return 5 + x;
}
public static void main(String[] args) {
System.out.println(myMethod(3));
}
}
//
OUTPUT
8
Example 2 (Multiple Parameters)
public class MyClass {
static int myMethod(int x, int y) {
return x + y;
}
public static void main(String[] args) {
System.out.println(myMethod(5, 3));
}
}
OUTPUT
8
Problem. Save your program as AverageMethod
◦Write a method named computeAverage() that holds two
parameters and computes the average of two numbers and
return the result.
◦Write another method named validateResult() that prints
HIGH if the average is above 50 and LOW, otherwise.
Solution:
public class AverageMethod{
public static void main(String[] args)
{ double average = computeAverage(78, 34);
System.out.println("The average is "+average);
validateResult(average);
}
public static double computeAverage(double num1, double num2)
{
return (num1+num2)/2;
}
public static void validateResult(double average)
{
if(average>50)
System.out.println("HIGH");
else
System.out.println("LOW");
}
}

More Related Content

PPTX
History Of JAVA
PPTX
Methods in java
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPT
Java interfaces
PPS
Introduction to class in java
PPTX
Interface in java
PPTX
Java Method, Static Block
PPTX
Core java complete ppt(note)
History Of JAVA
Methods in java
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Java interfaces
Introduction to class in java
Interface in java
Java Method, Static Block
Core java complete ppt(note)

What's hot (20)

PPTX
Exception handling
PDF
JavaScript - Chapter 8 - Objects
PPTX
I/O Streams
PDF
Arrays in Java
PPTX
Abstraction in java.pptx
PDF
Methods in Java
PPTX
OOPS In JAVA.pptx
PPSX
Java annotations
PDF
Basic Java Programming
PPT
Jsp ppt
PPTX
11. java methods
PPTX
Inheritance in java
PPT
Abstract class in java
PPT
Introduction to method overloading & method overriding in java hdm
PDF
Wrapper classes
PDF
Introduction to Java Programming
PDF
Java 8 Lambda Expressions
PPTX
JAVA AWT
PDF
PPTX
Java program structure
Exception handling
JavaScript - Chapter 8 - Objects
I/O Streams
Arrays in Java
Abstraction in java.pptx
Methods in Java
OOPS In JAVA.pptx
Java annotations
Basic Java Programming
Jsp ppt
11. java methods
Inheritance in java
Abstract class in java
Introduction to method overloading & method overriding in java hdm
Wrapper classes
Introduction to Java Programming
Java 8 Lambda Expressions
JAVA AWT
Java program structure
Ad

Similar to Working with Methods in Java.pptx (20)

PPTX
Methods In C-Sharp (C#)
PDF
static methods
PPTX
Computer programming 2 Lesson 15
DOCX
Methods in Java
PDF
oblect oriented programming language in java notes .pdf
PPTX
Lecture 4_Java Method-constructor_imp_keywords
PPTX
Java Foundations: Methods
PPTX
Module 4_CSE3146-Advanced Java Programming-Anno_Lambda-PPTs.pptx
PPTX
Java method present by showrov ahamed
PPTX
CJP Unit-1 contd.pptx
PPTX
1. Methods presentation which can easily help you understand java methods.pptx
PPT
M C6java3
PPT
Java
PPTX
Java method
PPT
Methods intro-1.0
PPTX
Polymorphism.pptx
PPTX
JAVA METHODS PRESENTATION WITH EXAMPLES DFDFFD FDGFDGDFG FGGF
PPT
Methods.ppt
Methods In C-Sharp (C#)
static methods
Computer programming 2 Lesson 15
Methods in Java
oblect oriented programming language in java notes .pdf
Lecture 4_Java Method-constructor_imp_keywords
Java Foundations: Methods
Module 4_CSE3146-Advanced Java Programming-Anno_Lambda-PPTs.pptx
Java method present by showrov ahamed
CJP Unit-1 contd.pptx
1. Methods presentation which can easily help you understand java methods.pptx
M C6java3
Java
Java method
Methods intro-1.0
Polymorphism.pptx
JAVA METHODS PRESENTATION WITH EXAMPLES DFDFFD FDGFDGDFG FGGF
Methods.ppt
Ad

Recently uploaded (20)

PDF
Digital Strategies for Manufacturing Companies
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Introduction to Artificial Intelligence
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
top salesforce developer skills in 2025.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ai tools demonstartion for schools and inter college
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Digital Strategies for Manufacturing Companies
Reimagine Home Health with the Power of Agentic AI​
Wondershare Filmora 15 Crack With Activation Key [2025
Introduction to Artificial Intelligence
Softaken Excel to vCard Converter Software.pdf
L1 - Introduction to python Backend.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
top salesforce developer skills in 2025.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
history of c programming in notes for students .pptx
Design an Analysis of Algorithms II-SECS-1021-03
Navsoft: AI-Powered Business Solutions & Custom Software Development
ai tools demonstartion for schools and inter college
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...

Working with Methods in Java.pptx

  • 1. WORKING WITH METHODS IN JAVA Computer Programming in Java
  • 2. At the end of this session, you are expected to: ◦Identify the different parts of a method in Java ◦Identify the different kinds of method ◦Create Java Methods
  • 3. 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.
  • 4. Create a Method ◦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 create your own methods to perform certain actions:
  • 5. Create a Method (example) public class MyClass { static void myMethod() { // code to be executed } }
  • 6. Example Explained ◦ myMethod() is the name of the method ◦ static means that the method belongs to the MyClass class and not an object of the MyClass class. You will learn more about objects and how to access methods through objects later in this tutorial. ◦ void means that this method does not have a return value. You will learn more about return values later in this chapter
  • 7. Call a Method ◦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:
  • 8. Call a Method (Example) public class MyClass { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } // Outputs "I just got executed!"
  • 9. Call a Method (Example) public class MyClass { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); //method call myMethod(); //method call myMethod(); //method call } }
  • 10. Java Method Parameters ◦ Parameters and Arguments ◦ Information can be passed to methods as parameter. Parameters act as variables inside the method. ◦ 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.
  • 11. Java Method Parameters (Example) public class MyClass { static void myMethod(String fname) { System.out.println(fname + " Refsnes"); } public static void main(String[] args) { myMethod("Liam"); myMethod("Jenny"); myMethod("Anja"); } } OUTPUT: Liam Refsnes Jenny Refsnes Anja Refsnes The 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:
  • 12. Argument ◦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
  • 13. Multiple Parameters public class MyClass { static void myMethod(String fname, int age) { System.out.println(fname + " is " + age); } public static void main(String[] args) { myMethod("Liam", 5); myMethod("Jenny", 8); myMethod("Anja", 31); } } OUTPUT Liam is 5 Jenny is 8 Anja is 31 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.
  • 14. Return Values ◦The void keyword, used in the examples above, 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:
  • 15. Example 1 (Single Parameter) public class MyClass { static int myMethod(int x) { return 5 + x; } public static void main(String[] args) { System.out.println(myMethod(3)); } } // OUTPUT 8
  • 16. Example 2 (Multiple Parameters) public class MyClass { static int myMethod(int x, int y) { return x + y; } public static void main(String[] args) { System.out.println(myMethod(5, 3)); } } OUTPUT 8
  • 17. Problem. Save your program as AverageMethod ◦Write a method named computeAverage() that holds two parameters and computes the average of two numbers and return the result. ◦Write another method named validateResult() that prints HIGH if the average is above 50 and LOW, otherwise.
  • 18. Solution: public class AverageMethod{ public static void main(String[] args) { double average = computeAverage(78, 34); System.out.println("The average is "+average); validateResult(average); } public static double computeAverage(double num1, double num2) { return (num1+num2)/2; } public static void validateResult(double average) { if(average>50) System.out.println("HIGH"); else System.out.println("LOW"); } }