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
1 2 3 |
modifier returnType methodName(parameters) { // Method body; } |
1 2 3 |
private int add(int num1,int num2) { return num1+num2; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
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; } } |
1 |
Result : 600 |
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:
1 2 3 |
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:
1 2 3 4 5 6 7 8 9 10 11 |
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
1 |
Result : 600 |
Returning a Value
As you can see from the above examples, both methods return a value.
1 2 3 |
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:
1 2 3 |
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.
1 2 3 4 5 6 7 8 |
public class MrMethod { public static void main(String[] args) { System.out.println("My name is : "+getName()); } private static String getName() { return "Oclemy"; } } |
Result
1 |
My name is : Oclemy |
Method Parameters
Methods can take parameters or arguments. And indeed the first example we saw can attest to that.
1 2 3 |
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.
1 2 3 4 5 6 7 8 |
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; } } |
1 |
Name: Swift Propellant: Plasma Ions |