A method is used to encpsulate a unit of reusable code.
Methods organize and simplify coding.
Method definition
A method definition comprises the following:
- Method name.
- Method Parameters.
- Return type.
- Method body
modifier returnType methodName(parameters) {
// Method body;
}
Let’s write a simple method to add two numbes:
private int add(int num1,int num2) {
return num1+num2;
}
In this case:
private
– access modifier for the method.int
– return type of the method.int num1,int num2
– method parameters.{ return num1+num2}
– method body.
Method Invokation
To call a method is also called to invoke a method.
Normally there are two situations you have to consider before you can successfully call a method.
- Instance Methods need an an instance of a class to be called.
- Static methods don’t need a class instance.
import java.util.Scanner;
public class MrMethod {
public static void main(String[] args) {
MrMethod mrMethod=new MrMethod();
System.out.println("Result : "+mrMethod.add(253,347));
}
private int add(int num1,int num2) {
return num1+num2;
}
}
Result
Result : 600
For example, in the above example, we need to instantiate MrMethod
class to be able to successfully invoke the add()
method.
Then we call the mrMethod.add(253,347)
to add.
On the other hand we can modify the method to become a static
method:
private static int add(int num1,int num2) {
return num1+num2;
}
We use the static
modifier. This now ties the method to the class
as opposed to the class instance
.
So we don’t need the instance of the class for successful invokation of this method:
import java.util.Scanner;
public class MrMethod {
public static void main(String[] args) {
System.out.println("Result : "+add(253,347));
}
private static int add(int num1,int num2) {
return num1+num2;
}
}
Result
Result : 600
Returning a Value
As you can see from the above examples, both methods return a value.
private static int add(int num1,int num2) {
return num1+num2;
}
This method has to return an integer given that already we’ve specified that it will only be capable of returing that in the method definition.
The below method returns a String:
private static String getName() {
return "Oclemy";
}
We’ve specified that it should return a String so the value we return has to be a string.
public class MrMethod {
public static void main(String[] args) {
System.out.println("My name is : "+getName());
}
private static String getName() {
return "Oclemy";
}
}
Result
My name is : Oclemy
Method Parameters
Methods can take parameters or arguments. And indeed the first example we saw can attest to that.
private static int add(int num1,int num2) {
return num1+num2;
}
In this case two integeres are passed: num1
and num2
.
The parameters passed to a method are only visible within the method body.
Methods can take parameters of any type.
public class MrMethod {
public static void main(String[] args) {
System.out.println(getSpacecraftDetails("Swift","Plasma Ions"));
}
private static String getSpacecraftDetails(String name,String propellant) {
return "Name: "+name+" Propellant: "+propellant;
}
}
Result
Name: Swift Propellant: Plasma Ions