Subject Name
Functions / methods in
Java
Dr. Anuprita Deshmukh
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Learning Objectives
▪ Need of functions/methods
▪ Writing and using static method
▪ How to pass values and return a value
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
What is function\method
Methods / functions of Java is a collection of statements that perform
some specific task and return the result to the caller.
Advantage of Method
• Code Reusability
• Code Optimization
• Improved readability
• Encapsulation
• Separation of concerns
• Improved testability
• Improved modularity
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Method declaration
class helloworld
{
1. Modifier public static void main(String args[])
2. The return type {
3. Method Name System.out.println("Hello,
World!");
4. Parameter list
System.out.println("Hi...."+"
5. Exception list Anuprita");
6. Method body }
}
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Ways to create method in Java
▪ Instance method
▪ Static method
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Types of method
▪ Predefined method
▪ User method
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Static method
public class Main
{
static void MyFirstMethod(ParameterList)
{
// code to be executed
}
}
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Calling function
class Math1
{
public static void main(String args[])
{
System.out.println(“Hello");
Greating();
System.out.println(“ EveryOne");
}
public static void Greating()
{
System.out.println("Welcome");
}
}
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Calling function with parameter
class Math1 {
public static void main(String args[]) {
int a = 25;
int b = 24;
EvenOdd(a);
EvenOdd(b);
}
public static void EvenOdd(int x) {
int r = x % 2;
if (r == 0)
System.out.println("The Number is Even");
else
System.out.println("The Number is Odd");
}
}
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Returning value from function
class Math1
{
public static void main(String args[])
{
int a = 5;
int S1;
S1 = Square(a);
System.out.println("The Square of " + a + " is " + S1);
}
public static int Square(int n)
{
return n * n;
}
}
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Method overloading
In java multiple methods can have the same name with
different parameters
int Add(int x, int y)
int Add(int x, int y, int z)
103 Java Programming (Dr Anuprita Deshmukh)
1.3 Functions\methods in Java
Subject Name
Method Overloading
class Math1 {
public static void main(String args[]) {
int a = 5;
int b = 4;
int c = 7;
int ans1, ans2;
ans1 = Add(a, b);
ans2 = Add(a, b, c);
System.out.println("The First Addition is " + ans1 + “ The Addition is " + ans2);
}
public static int Add(int n1, int n2) {
return n1 + n2;
}
public static int Add(int n1, int n2, int n3) {
return n1 + n2+n3;
}
}
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
Quiz
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
What are the parts of Java?
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
What are the parts of method in Java?
1. Modifier
2. The return type
3. Method Name
4. Parameter list
5. Exception list
6. Method body
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
What is the return type of a method that does
not return any value?
A. Int
B. Float
C. Void
D. double
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
What is the return type of a method that does not
return any value?
A. Int
B. Float
C. Void
D. double
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
Which method can be defined only once in a
program
A. main method
B. finalize method
C. static method
D. private method
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
What is the process of defining more than one method in
a class differentiated by method signature?
A. Function overriding
B. Function overloading
C. Function doubling
D. None of the mentioned
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
What is the process of defining more than one
method in a class differentiated by method
signature? A. Function overriding
B. Function overloading
C. Function doubling
D. None of the mentioned
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
Summary
▪ We discussed the Need of methods
▪ Learned how to Write static method
▪ Learned how to pass values and return
a value from the function
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java
Subject Name
Thank you
103 Java programming (Dr Anuprita Deshmukh)
1.3 functions\methods in Java