20 Jul Java Methods
A method is an organized block of reusable code, that avoids repeating the tasks again and again. If you want to do a task repeatedly in a code, then just make a method, set the task in it, and call the method multiple times whenever you need it. Methods are also known as Functions.
Create and call a Method
To create a function in Java, set the name of the function followed by parentheses. Our method is demoMethod():
1 2 3 4 5 |
public static void demoMethod() { // write the code } |
A function in Java gets executed when it is called:
1 2 3 |
demoMethod() |
Let us now see an example of creating a method in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Studyopedia { // Demo method static void demoMethod() { System.out.println("This is a demo method in Java"); } public static void main(String[] args) { // Calling the method demoMethod(); } } |
Output
1 2 3 |
This is a demo method in Java |
Method Declaration and Definition
In the above example, we saw how to create and call a method in Java. We defined and declared the method as well:
1 2 3 4 5 |
public static void demoMethod() { // method declaration // method definition } |
A method in Java gets executed when it is called:
1 2 3 |
demoMethod() |
Method Parameters
Set the parameters in a Java method after the name of the method. For example:
1 2 3 |
static void demoMethod(int rank) { } |
To call a method with a parameter, set the value of the variable while calling, for example:
1 2 3 |
demoMethod(5) |
Therefore, we have passed the rank with the value 5 while calling above.
Let us now see an example of creating a Method and passing a parameter:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Studyopedia { // Demo method static void demoMethod(int rank) { System.out.println("Rank = "+rank); } public static void main(String[] args) { // Calling the method demoMethod(5); } } |
Output
1 2 3 |
Rank = 5 |
Multiple Parameters
We can also set more than one parameter in a function in Java. For example:
1 2 3 |
static void demoMethod(string player, int rank, int points) |
To call a function with multiple parameters, set the value of the variable while calling, for example:
1 2 3 |
static void demoMethod("Amit", 1, 95) |
We have passed the following multiple parameters while calling above:
- player: Amit
- rank: 1
- points: 95
Let us now see an example of creating a Method and passing multiple parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Studyopedia { // Demo method with multiple parameters static void demoMethod(String player, int rank, int points) { System.out.println("Player = "+player); System.out.println("Rank = "+rank); System.out.println("Points = "+points); } public static void main(String[] args) { // Calling the method demoMethod("Amit", 1, 95); demoMethod("Jack", 2, 85); demoMethod("John", 3, 80); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Player = Amit Rank = 1 Points = 95 Player = Jack Rank = 2 Points = 85 Player = John Rank = 3 Points = 80 |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments