Methods in Java
- Introduction of method/funtion
Method is a process to perform specific Task
Example :
- methods:
- electrician()
- plumber()
- builder()
- painting()
- wooden work()
Syntax :
access_modifier return_type method_name() {
method body(code to perform the task)
}
- method_name: This is the name of the method. It should follow Java naming conventions .
- Method body: It contains the statements and logic to perform the desired task.
- return_type: This specifies the data type of the value that the method will return.
Methods
- A method is a self-contained Block of statement that perform a specific task of some kind.
- Every Java program has one or more methods in it.
method (call and definition):
public class manvikfun {
public static void main(String[] args) {
gym();//method call
System.out.println("hi i am manvik");
}
static void gym(){//method defination
System.out.println("i am in gym ");
}
}
# Out put:
i am in gym
hi i am manvik
here,
We define two methods -main() and gym() and used gym word two time. Let us understand meaning of each .
First gym()
static void gym()//method defination
{
System.out.println("i am in gym ");
}
- This is method definition.
Second gym()
gym();//method call
here,
main() call gym()
- When we say that main() call the method gym() we mean that control passes to method gym() then method going to work , after the execution of method control return to main() which come to life again and begins executing its code at the exit point.
- main() become a calling function.
- gym() become the called method(function).
Call more then one method
- Java can contain one or more classes each class can contain one or more methods.
- execution always begins with main() function present in class that class is main class.
- Class must contain main() because program execution always start with main() function.
- sequence execution.
public class manvikfun {
public static void main(String[] args) {
gym();//method call
benchpress();//second call
squats();//third call
deadlift();//fourth call
System.out.println("hi i am manvik");
}
static void gym(){//method defination
System.out.println("i am in gym ");
}
static void benchpress(){//method defination
System.out.println(" chest day ");
}
static void squats(){//method defination
System.out.println("leg day");
}
static void deadlift(){//method defination
System.out.println("back day");
}
}
#Output
i am in gym
chest day
leg day
back day
hi i am manvik
One method call another method
Any method can call from any other method. Even main() can be called from other method.
public class manvikfun {
public static void main(String[] args) {
System.out.println("Before-START-");
gym();//method call
System.out.println("after-END-");
}
static void gym(){//method defination
System.out.println("i am in gym-START-");
benchpress();
System.out.println("i am in gym-END-");
}
static void benchpress(){//method defination
System.out.println(" chest day-START-");
squats();
System.out.println(" chest day-END-");
}
static void squats(){//method defination
System.out.println("leg day-START-");
deadlift();
System.out.println(" leg day-END-");
}
static void deadlift(){//method defination
System.out.println("back day");
}
}
#Output
Before-START-
i am in gym-START-
chest day-START-
leg day-START-
back day
leg day-END-
chest day-END-
i am in gym-END-
after-END-
Why use methods
- Code Reusability
- Abstraction
- Modularity
- Encapsulation
- Code Readability
Pass by between methods(Parameters and Arguments):
- Information can be passed to methods as parameter.
- Parameters are specified after the method name, inside the parentheses
- You can add as many parameters as you want
# Syntax:
access_modifier return_type method_name(parameters) {
method body(code to perform the task)
}
# Example:
import java.util.Scanner;
public class para {
public static void main(String[] args) {
int add = 0;
Scanner sc = new Scanner(System.in);
System.out.println("enter any three numbers");
System.out.println("a = ");
int a = sc.nextInt();
System.out.println("b = ");
int b = sc.nextInt();
System.out.println(" = ");
int c = sc.nextInt();
System.out.println("Sum = "+sum(a, b, c ));
}
static int sum(int x, int y, int z ){
return x + y + z;
}
}
- To pass the values of a, b, c to the method sum(), while making a call to the method sum() we have mentioned a, b, c in the parenthesis.
sum(a, b, c);
- In the sum() method these values get collected in three variables x, y, z.
int sum( int x, int y, int z)
Note:
actual arguments ( a, b, c) also called just arguments
formal arguments (x , y , z) also called parameters
- Quiz:
warning ⚠ - one time quiz
Timer Quiz: Quiz Link
Non-Timer Quiz: non-timer Quiz Link
Comments
Post a Comment