What is a Method?
In Java, a method is a block of code that performs a specific function and runs only when it is called. Methods are also commonly known as functions. Each method has its name. You can pass data into a method via parameters. A method also has a return type defining the type of data it returns. According to the convention, the name of the method should be written in lowerCamelCase where the first letter should be small. Furthermore, a method should have a proper name, preferably a verb referring to what it does e.g. add(), printContactList(), updateInfo() etc. Every time a program encounters a method call, the program execution branches out to the body of the method. The body code runs and the method returns to the previous code from which it was called, and continues from the next line. A method returns to the code that invoked it when:- It completes all the code in the method and reaches the end of it.
- It reaches a return statement.
- It throws an exception.
Why are Methods used?
Methods are used because they allow code to be reused without rewriting it again and again. Methods are timesavers and keep the code organized and readable. It makes the code understandable to multiple coders. It helps in modularizing the program. If methods are not used the program can become extremely lengthy and difficult to test, debug or maintain the code.Create a Method
public class Driver {
public static void printName(String name) {
System.out.println("Hi, I am " + name + "!");
}
}
Method declaration
In general, method declaration has the following components:Modifier: Defines the access type i.e. from where the method can be accessed in your program e.g. public, private, etc. It is public in this case, which means this method can be accessed outside of the class too.
Return Type: The data type of the value that the method returns. In this case, it is void i.e. does not return anything.
Method Name: It is the name of the method by which it will be called in our program. The name of our method is printName.
Parameter List: It is the list of data that needs to be passed into the method. It is comma-separated and each input data is preceded by its datatype. If there is no data to be passed the brackets () are left empty. We have passed one parameter name of type String.
Method body: It consists of the code that needs to be executed enclosed within curly braces {}.
Call a Method
To call a method in Java, simply write the method’s name followed by two parentheses () and a semicolon(;). If the method has parameters in the declaration, those parameters are passed within the parentheses () but this time without their datatypes specified. However, it is important to keep the sequence of arguments the same as defined in the method definition. Let’s look at an example to understand this better.Example 1
public class Driver {
public static void printName(String name) {
System.out.println("Hi, I am " + name + "!");
}
public static void main(String[] args) {
String name = "Mary";
printName(name);
String name1 = "Lucy";
printName(name1);
String name2 = "Alex";
printName(name2);
String name3 = "Zoey";
printName(name3);
}
}
Output
Explanation
In the snippet above, the method we defined is called in the main. It has one argument that needs to be passed. We have called the method four times, each time changing the argument. With all four different arguments, the method has returned different outputs for different names.Example 2
public class Driver {
static int add(int x, int y) {
int sum = x + y;
return sum;
}
public static void main(String[] args) {
int x = 10;
int y = 20;
int z = add(x, y);
System.out.println(x + " + " + y + " = " + z);
x = 5;
y = 4;
z = add(x, y);
System.out.println(x + " + " + y + " = " + z);
x = 100;
y = 15;
z = add(x, y);
System.out.println(x + " + " + y + " = " + z);
x = 50;
y = 5;
z = add(x, y);
System.out.println(x + " + " + y + " = " + z);
}
}
GO TO FULL VERSION