SlideShare a Scribd company logo
Java Methods
Dr. M H B Ariyaratne
https://goo.gl/T7qtAS
What are Methods?
● A set of code / Collection of Statements
● Group together to perform an operation
● Referred to by name
● Called / Invoked by utilizing the method's name
● Like a subprogram that acts on data and often returns a value
● When method name is encountered in a program, the execution of the
program branches to the body of that method. When the method is finished,
execution returns to the area of the program code from which it was called,
and the program continues on to the next line of code.
11. java methods
Advantages
Time savers
Allow for the repetition of sections of code without retyping the code
Saved and utilized again and again in newly developed programs
Allows several programmers to work independently on separate concepts which
can be assembled at a later stage
Syntax
public static int methodName(int a, int b) {
// body
}
● public static − modifier
● int − return type
● methodName − name of the method
● int a, int b − list of parameters
Example
/** the snippet returns the minimum between two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
Invoking a Method
public static void main(String[] args) {
int a = 11;
int b = 6;
int c = minFunction(a, b);
System.out.println("Minimum Value = " + c);
}
The void
Keyword
public static void main(String[]
args) {
printGrade(55);
printGrade(85);
}
public static void printGrade(double marks) {
if (marks >= 90) {
System.out.println("A+");
}else if (marks >= 80) {
System.out.println("A");
}else if (marks >= 70) {
System.out.println("B+");
}else if (marks >= 60) {
System.out.println("B");
}else if (marks >= 50) {
System.out.println("S");
}else {
System.out.println("F");
}
}
Method Overloading
A class has two or more methods by the same name but different parameters
It is different from overriding. In overriding, a method has the same method
name, type, number of parameters, etc.
The Constructors
● Initializes an object when it is created
● Has the same name as its class
● Syntactically similar to a method
● Have no explicit return type.
● Use to give initial values to the instance variables defined by the class, or to
perform any other startup procedures required to create a fully formed
object.
● All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member
variables to zero. However, once you define your own constructor, the
default constructor is no longer used.
Parameterized Constructor
A constructor that accepts one or more parameters.
Parameters are added to a constructor in the same way that they are added to a
method, just declare them inside the parentheses after the constructor's name.
This keyword
this is a keyword in Java which is used as a reference to the object of the current
class, with in an instance method or a constructor. Using this you can refer the
members of a class such as constructors, variables and methods.
Thank you

More Related Content

PPT
Abstract class in java
PPTX
Methods in java
PPTX
Dynamic method dispatch
PPT
Method overriding
PDF
Methods in Java
PPTX
Method overloading
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
java interface and packages
Abstract class in java
Methods in java
Dynamic method dispatch
Method overriding
Methods in Java
Method overloading
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
java interface and packages

What's hot (20)

PDF
Life cycle-of-a-thread
PDF
Arrays in Java
PPTX
Array of objects.pptx
PPTX
Classes, objects in JAVA
PDF
Java Serialization
PDF
itft-Decision making and branching in java
PPTX
Strings in Java
PPTX
Data types in java
PPTX
Stack using Array
PPTX
Java program structure
PPSX
Data Types & Variables in JAVA
PPTX
Getters_And_Setters.pptx
PPTX
Java constructors
PDF
Class and Objects in Java
PPTX
Abstract class and Interface
PPTX
Polymorphism presentation in java
PPT
Java static keyword
DOCX
Recursion in C++
PPTX
Inheritance In Java
Life cycle-of-a-thread
Arrays in Java
Array of objects.pptx
Classes, objects in JAVA
Java Serialization
itft-Decision making and branching in java
Strings in Java
Data types in java
Stack using Array
Java program structure
Data Types & Variables in JAVA
Getters_And_Setters.pptx
Java constructors
Class and Objects in Java
Abstract class and Interface
Polymorphism presentation in java
Java static keyword
Recursion in C++
Inheritance In Java
Ad

Similar to 11. java methods (20)

DOCX
Methods in Java
PPTX
Chapter 6.6
PPT
Class & Object - User Defined Method
PPTX
OOPSCA1.pptx
PPTX
Computer programming 2 Lesson 15
PPTX
Basic concept of class, method , command line-argument
PPTX
Working with Methods in Java.pptx
PPTX
Chap-2 Classes & Methods.pptx
DOCX
Class
PPTX
1. Methods presentation which can easily help you understand java methods.pptx
PDF
Week 7 Java Programming Methods For I.T students.pdf
PDF
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
DOCX
The Java Learning Kit Chapter 5 – Methods and Modular.docx
PPTX
Lecture 4_Java Method-constructor_imp_keywords
PPTX
CJP Unit-1 contd.pptx
PPT
java Methods and data Constructors (2).ppt
PPTX
Lecture_4_Static_variables_and_Methods.pptx
PPTX
Java Method Presentation - Java Programming NC III.pptx
PPTX
Java method
PPT
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
Methods in Java
Chapter 6.6
Class & Object - User Defined Method
OOPSCA1.pptx
Computer programming 2 Lesson 15
Basic concept of class, method , command line-argument
Working with Methods in Java.pptx
Chap-2 Classes & Methods.pptx
Class
1. Methods presentation which can easily help you understand java methods.pptx
Week 7 Java Programming Methods For I.T students.pdf
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
The Java Learning Kit Chapter 5 – Methods and Modular.docx
Lecture 4_Java Method-constructor_imp_keywords
CJP Unit-1 contd.pptx
java Methods and data Constructors (2).ppt
Lecture_4_Static_variables_and_Methods.pptx
Java Method Presentation - Java Programming NC III.pptx
Java method
123 JAVA CLASSES, OBJECTS AND METHODS.ppt
Ad

More from M H Buddhika Ariyaratne (17)

PPTX
14. collections
PPTX
13. interfaces
PPTX
PPTX
10. inheritance
PPTX
PPTX
8. objects & classes
PPTX
7. flowcharts and pseudocode
PPTX
6. java operators
PPTX
5. variables & data types
PPTX
4. mathematics for computing
PPTX
3. introduction to java
PPTX
2. data structures & algorithms
PPTX
1. introduction mathematics for computing and object oriented programming
PPTX
Gis for healthcare introduction
PPTX
Open hospital management information system
PPTX
Who am i and what i have done
PPTX
Electronic health record system for sri lankan general practitioners
14. collections
13. interfaces
10. inheritance
8. objects & classes
7. flowcharts and pseudocode
6. java operators
5. variables & data types
4. mathematics for computing
3. introduction to java
2. data structures & algorithms
1. introduction mathematics for computing and object oriented programming
Gis for healthcare introduction
Open hospital management information system
Who am i and what i have done
Electronic health record system for sri lankan general practitioners

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
L1 - Introduction to python Backend.pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
medical staffing services at VALiNTRY
PDF
top salesforce developer skills in 2025.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
ai tools demonstartion for schools and inter college
PDF
System and Network Administraation Chapter 3
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Reimagine Home Health with the Power of Agentic AI​
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PTS Company Brochure 2025 (1).pdf.......
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
wealthsignaloriginal-com-DS-text-... (1).pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Adobe Illustrator 28.6 Crack My Vision of Vector Design
L1 - Introduction to python Backend.pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Softaken Excel to vCard Converter Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
medical staffing services at VALiNTRY
top salesforce developer skills in 2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
ai tools demonstartion for schools and inter college
System and Network Administraation Chapter 3
Design an Analysis of Algorithms I-SECS-1021-03
Reimagine Home Health with the Power of Agentic AI​

11. java methods

  • 1. Java Methods Dr. M H B Ariyaratne https://goo.gl/T7qtAS
  • 2. What are Methods? ● A set of code / Collection of Statements ● Group together to perform an operation ● Referred to by name ● Called / Invoked by utilizing the method's name ● Like a subprogram that acts on data and often returns a value ● When method name is encountered in a program, the execution of the program branches to the body of that method. When the method is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code.
  • 4. Advantages Time savers Allow for the repetition of sections of code without retyping the code Saved and utilized again and again in newly developed programs Allows several programmers to work independently on separate concepts which can be assembled at a later stage
  • 5. Syntax public static int methodName(int a, int b) { // body } ● public static − modifier ● int − return type ● methodName − name of the method ● int a, int b − list of parameters
  • 6. Example /** the snippet returns the minimum between two numbers */ public static int minFunction(int n1, int n2) { int min; if (n1 > n2) min = n2; else min = n1; return min; }
  • 7. Invoking a Method public static void main(String[] args) { int a = 11; int b = 6; int c = minFunction(a, b); System.out.println("Minimum Value = " + c); }
  • 8. The void Keyword public static void main(String[] args) { printGrade(55); printGrade(85); } public static void printGrade(double marks) { if (marks >= 90) { System.out.println("A+"); }else if (marks >= 80) { System.out.println("A"); }else if (marks >= 70) { System.out.println("B+"); }else if (marks >= 60) { System.out.println("B"); }else if (marks >= 50) { System.out.println("S"); }else { System.out.println("F"); } }
  • 9. Method Overloading A class has two or more methods by the same name but different parameters It is different from overriding. In overriding, a method has the same method name, type, number of parameters, etc.
  • 10. The Constructors ● Initializes an object when it is created ● Has the same name as its class ● Syntactically similar to a method ● Have no explicit return type. ● Use to give initial values to the instance variables defined by the class, or to perform any other startup procedures required to create a fully formed object. ● All classes have constructors, whether you define one or not, because Java automatically provides a default constructor that initializes all member variables to zero. However, once you define your own constructor, the default constructor is no longer used.
  • 11. Parameterized Constructor A constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor's name.
  • 12. This keyword this is a keyword in Java which is used as a reference to the object of the current class, with in an instance method or a constructor. Using this you can refer the members of a class such as constructors, variables and methods.