Java - Methods
A method also known as function is a block of statements which executes only when it is called somewhere in the program. A method provides re-usability of same code for different inputs, hence saves time and resources. Java has many built-in methods and one of the common method is System.out.println() which is used to print the output in a new line. Users can also create their own method which is also termed as user-defined methods.
Create method
In Java, a method must be declared in a class. Creating a method starts with modifier followed by return type of method. After that, it is followed by method's name and parenthesis containing method's parameter(s), if it has any. At last, it contains block of statements which is also called body of the method.
Syntax
//Defining method modifier return_type method_name(parameters) { statements; }
modifier: It defines the access type of the method and it is optional to use. There are four types of access modifier in Java.
- public
- protected
- private
- default: When no modifier is specified
The following table shows the access to members permitted by each modifier.
Modifier | Class | Package | Subclass | World |
---|---|---|---|---|
public | Y | Y | Y | Y |
protected | Y | Y | Y | N |
default | Y | Y | N | N |
private | Y | N | N | N |
return_type: A method can return value(s). The return_type is the data type of the value the method returns. If a method does not return anything in that case void is used as return_type.
Example: A method with no parameter
In the example below, a method HelloWorld is created to print Hello World!. It requires no parameters to execute and has no return type, hence void keyword is used.
static tells the program that the HelloWorld belongs to the MyClass and not an object of the MyClass.
public class MyClass { static void HelloWorld() { System.out.println("Hello World!"); } public static void main(String[] args) { HelloWorld(); } }
The output of the above code will be:
Hello World!.
Call a method
After defining the method, it can be called anywhere in the program with it's name followed by parenthesis containing method's parameter(s), if it has any and semicolon ;. In the above example, the method is called inside main() method using the following statement:
HelloWorld();
Parameter
A parameter (or also known as argument) is a variable which is used to pass information inside a method. In above example, the method does not has any parameter. But a user can create a method with single or multiple parameters. Value of a parameter can be further used by the method to achieve desired result.
Example: A method with parameters
In the example below, the method called MyMethod is created which requires two integer numbers as parameters and print sum of two numbers in desired style. Please note that, the method returns nothing hence void is used as return type.
public class MyClass { static void MyMethod(int a, int b) { int sum; sum = a + b; System.out.println("Sum of "+ a +" and "+ b +" is: "+ sum); } public static void main(String[] args) { MyMethod(15, 10); } }
The output of the above code will be:
Sum of 15 and 10 is: 25
Return Values
A method can be used to return values. To achieve this, user must have to define return type in definition and declaration of the method. In the example below, the return type is int.
public class MyClass { static int MyMethod(int a, int b) { return (a + b); } public static void main(String[] args) { int x = 15, y = 10, sum; sum = MyMethod(x, y); System.out.println("Sum of "+ x +" and "+ y +" is: "+ sum); } }
The output of the above code will be:
Sum of 15 and 10 is: 25