Methods in Java
When you start programming in Java, you should know that there are many new concepts to learn: classes, methods, exceptions, constructors, variables and more, and it can become overwhelming. So, it's best to learn slowly. In this article, we will: study methods and how to call a method in Java.
A Java method is equivalent to a function in C language that encompasses a set of operations in a single block whose purpose is to optimize and reduce code. This method can be called from other methods and when it is called, all methods inside that method will also be called and executed. For example, when you call the System.out.println(), the system performs several operations just to display a message in the console.
Now you'll learn how to create your own methods with or without returns, call a method with or without parameters, and optimize the abstraction of code in a method.
- The second keyword, static means that the method belongs to the class and instance of the class (object). The static should be called using the class name: "ExampleClass.ExampleMethod()". However, if the keyword static is not there, so the method can be called only through an object. If the class has a constructor (for creating objects), then you can instantiate the class by typing ExampleObject obj = new ExampleObject(); , and call the method with "obj. ExampleMethod(); ".
- int is the return type of the function, in which case the method is declared as a function that returns an integer result after processing is performed. The keyword to return the result is return.
- int a, int b are the list of parameters that will be used inside the method, for example we want to do an operation of adding a and b and put the result in a sum variable that will be returned as output with the statement return sum; .
Example:
Here is a code to compare two numbers and make the maximum value. The function takes two parameters nb1 and nb2 and returns the maximum of both:
Note that the System.out.println() doesn't return anything because it's of type void.
1- The statement return is executed (function).
2- We reach the closing parenthesis without returning anything (procedure).
You can use what the function returns as shown in this example:
Passing parameters by value means calling a method with a parameter. The following program shows an example of how to pass by value. The values of the arguments remain the same after calling the method.
Let's consider the example seen earlier that determines the maximum between two integers. Let's say we want to determine the maximum number of a double type. The concept of overloading will help us create two or more methods with the same name, but different parameters.
A Java method is equivalent to a function in C language that encompasses a set of operations in a single block whose purpose is to optimize and reduce code. This method can be called from other methods and when it is called, all methods inside that method will also be called and executed. For example, when you call the System.out.println(), the system performs several operations just to display a message in the console.
Now you'll learn how to create your own methods with or without returns, call a method with or without parameters, and optimize the abstraction of code in a method.
Creating a method
Let's consider the following example to understand the structure of a method:
public static int methodname(int a, int b) {
// method body
int sum = a + b;
return sum;
}
- The keyword public means that the method itself can To be called from any block, different classes, or even different packages as long as you import the calling class. There are three other keywords that can replace public. These are protected and private. If the method is protected, only then can this class and the lower classes that inherit from it call the method. If the method is private, then the method can To be called only within the classroom.
- The second keyword, static means that the method belongs to the class and instance of the class (object). The static should be called using the class name: "ExampleClass.ExampleMethod()". However, if the keyword static is not there, so the method can be called only through an object. If the class has a constructor (for creating objects), then you can instantiate the class by typing ExampleObject obj = new ExampleObject(); , and call the method with "obj. ExampleMethod(); ".
- int is the return type of the function, in which case the method is declared as a function that returns an integer result after processing is performed. The keyword to return the result is return.
- int a, int b are the list of parameters that will be used inside the method, for example we want to do an operation of adding a and b and put the result in a sum variable that will be returned as output with the statement return sum; .
Example:
Here is a code to compare two numbers and make the maximum value. The function takes two parameters nb1 and nb2 and returns the maximum of both:
public static int max(int a, int b) {
int max;
if(a> b)
max=a;
else
max=b;
return max;
}
The void keyword
The word void indicates that the method does not return anything when you call the method. If you want a method to return something, then replace the word void by the data type of the object you want to return. Then add the word return plus the object.
In this example we consider a procedure that takes a int n as a parameter and prints the list of numbers from 1 to n. This method is of type void and does not return a value.
public class main {Runtime:
public static void main(String[] args) {
print(5);
}
public static void print(int n){
for(int i = 0; i < n; i++)
System.out.println(i);
}
}
0
1
2
3
4
How to call a method
To use a method, you need to call it. There are two ways in which a method is called. Processing a method call is simple. When a program invokes a method, the Control is transferred to the method called. After the method is finished, the control is returned to the caller with two conditions:1- The statement return is executed (function).
2- We reach the closing parenthesis without returning anything (procedure).
You can use what the function returns as shown in this example:
public class main {Runtime:
public static void main(String[] args) {
int a, b;
a = 5;
b = 10;
int result = sum(a, b);
System.out.println("a+b = "+result);
}
public static int sum(int a, int b){
return a+b;
}
}
a+b = 15
Passing Parameters by Value
The arguments that are passed must exactly match the order of the parameters declared in the method. Parameters can be passed by value or by reference.Passing parameters by value means calling a method with a parameter. The following program shows an example of how to pass by value. The values of the arguments remain the same after calling the method.
public class main {Runtime:
public static void main(String[] args) {
int a = 30;
int b = 45;
System.out.println("Before the swap (main), a = " + a
+ " b = " + b);
//invoke the permute()
permute(a, b) method;
System.out.println("After the swap (main), a = " + a
+ " b = " + b);
}
public static void permuter(int a, int b) {
System.out.println("Before permutation (method), a = " + a
+ " b = " + b);
// swap a with b
int c = a;
a = b;
b = c;
System.out.println("After the swap (method), a = " + a
+ " b = " + b);
}
}
Before the swap (main), a = 30 b = 45
Before the swap (method), a = 30 b = 45
After the swap (method), a = 45 b = 30
After the swap (hand), a = 30 b = 45
Method Overloading in java
When a class contains two or more methods with the same name but different parameters, this is known as method overloading. it is different from overriding where the method has the same name, type, number of parameters, etc.Let's consider the example seen earlier that determines the maximum between two integers. Let's say we want to determine the maximum number of a double type. The concept of overloading will help us create two or more methods with the same name, but different parameters.
public class main {Runtime:
public static void main(String[] args) {
int a, b;
a = 7;
b = 8;
System.out.println("max("+a+", "+ b+") = "+max(a, b));
double x,y;
x=1.5;
y=4.2;
System.out.println("max("+x+", "+ y+") = "+max(x, y));
}
public static int max(int a, int b) {
int max;
if(a> b)
max=a;
else
max=b;
return max;
}
public static double max(double a, double b) {
double max;
if(a> b)
max=a;
else
max=b;
return max;
}
}
max(7, 8) = 8The method overload makes the program adaptable. Here, the two methods have the same name, but different parameters. The maximum number of integer and double types is the result.
max(1.5, 4.2) = 4.2