20 Jul Java Methods
A method is an organized block of reusable code that avoids repeating tasks again and again. If you want to do a task repeatedly in 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.
Types of Methods in Java
- Pre-defined: Eg: println(), concat(), equals(), etc.
- User-defined: Custom methods created by the programmer for custom tasks.
Let us see how to:
- Create and call a method.
- Method Declaration and Definition
- Method Parameters
- Multiple Parameters
Create and call a Method in Java – Example 1
To create a function in Java, set the name of the function followed by parentheses. Our method is demoMethod():
public static void demoMethod() {
// write the code
}
A function in Java gets executed when it is called:
demoMethod()
Let us now see an example of creating a method in Java:
// Create and call a Function (Method) in Java
class Studyopedia {
/* We add the static keyword in Java so that a method belongs to the class itself,
rather than to individual objects created from that class */
/* A static method is tied to the class itself. Even if no object of Demo105 exists,
the method can still be executed.
*/
// Function Definition
static void demoMethod() {
System.out.println("This is a demo method in Java.");
}
public static void main(String[] args) {
// Function call
demoMethod();
}
}
Output
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:
public static void demoMethod() { // method declaration
// method definition
}
A method in Java gets executed when it is called:
demoMethod()
Method Parameters in Java – Example 2
Set the parameters in a Java method after the name of the method. For example:
static void demoMethod(int rank) { }
To call a method with a parameter, set the value of the variable while calling, for example:
demoMethod(5)
Therefore, we have passed the rank with the value 5 while calling the above.
Let us now see an example of creating a Method and passing a parameter in Java:
// Function (Method) Parameters in Java
class Studyopedia {
/* We add the static keyword in Java so that a method belongs to the class itself,
rather than to individual objects created from that class */
/* A static method is tied to the class itself. Even if no object of class Studyopedia exists,
the method can still be executed.
*/
// Function Definition with rank parameter
static void demoMethod(int rank) {
System.out.println("This is a demo method in Java.");
System.out.println("Rank = "+rank);
}
public static void main(String[] args) {
// Function call with 5 as an argument
demoMethod(5);
}
}
Output
This is a demo method in Java. Rank = 5
Multiple Parameters in a Method in Java
We can also set more than one parameter in a function in Java. For example:
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:
demoMethod("Amit", 1, 95)
We have passed the following multiple parameters while calling the above:
- player: Amit
- rank: 1
- points: 95
Let us now see an example of creating a Method and passing multiple parameters:
// Function (Method) with Multiple Parameters in Java
class Demo107 {
/* We add the static keyword in Java so that a method belongs to the class itself,
rather than to individual objects created from that class */
/* A static method is tied to the class itself. Even if no object of Demo105 exists,
the method can still be executed.
*/
// Function Definition with player, rank, and points parameter
static void demoMethod(String player, int rank, int points) {
System.out.println("\nThis is a demo method in Java.");
System.out.println("Player = "+player);
System.out.println("Rank = "+rank);
System.out.println("Points = "+points);
}
public static void main(String[] args) {
// Function call with 3 arguments
demoMethod("Amit", 1, 95);
demoMethod("Steve", 2, 90);
demoMethod("David", 3, 80);
}
}
Output
This is a demo method in Java. Player = Amit Rank = 1 Points = 95 This is a demo method in Java. Player = Steve Rank = 2 Points = 90 This is a demo method in Java. Player = David 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