dsa

Java Methods

Introduction :

In this tutorial, you will learn about Java methods, how to define methods, and how to use methods in Java programs with the help of examples.

A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class which is different from languages like C, C++, and Python.

What is a Method ?

A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design.

Types of methods :

Depending on whether a method is defined by the user, or available in the standard library, there are two types of methods in Java:

How to call a Method in Java ?

To call a method in Java, you have to write the method’s name followed by parentheses () and a semicolon ;

Example :


myMethod();
Working of the method call in Java
fig: Working of the method call in Java

Example :


class Main {

    public static void main(String[] args) {
        System.out.println("About to encounter a method.");

        // method call
        myMethod();

        System.out.println("Method was executed successfully!");
    }

    // method definition
    private static void myMethod(){
        System.out.println("Printing from inside myMethod()!");
    }
}
Output :

About to encounter a method.
Printing from inside myMethod().
Method was executed successfully!

Method Parameters and Return Value

Parameters are specified after the method name in a class, inside parentheses. You can add as many parameters as you want but just separate them with a comma. Data can be passed to functions as a parameter. Actually, parameters act as variables inside the method.

Example for Passing Parameters :


public class swappingExample
{
   	public static void main(String[] args) 
	{
		int a = 30;
      		int b = 45;
      		System.out.println("Before swapping, a = " + a + " and b = " + b);
      		// Invoke the swap method
      		swapFunction(a, b);
      		System.out.println("\n**Now, Before and After swapping values will be same here**:");
      		System.out.println("After swapping, a = " + a + " and b is " + b);
   	}
   	public static void swapFunction(int a, int b) 
	{
      		System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
      		// Swap n1 with n2
      		int c = a;
      		a = b;
      		b = c;
      		System.out.println("After swapping(Inside), a = " + a + " b = " + b);
   	}
}
Output :

Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30

**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45

Example: Method Accepting Arguments and Returning Value


public class Main 
{   
    	public static void main(String[] args) 
	{
	        int result, n;        
        	n = 3;
        	result = square(n);
        	System.out.println("Square of 3 is: " + result);
	        n = 4;
        	result = square(n); 
        	System.out.println("Square of 4 is: " + result);
    	}
    	// method 
    	static int square(int i) 
	{
	        return i * i;
	}
}
Output :

Squared value of 3 is: 9
Squared value of 4 is: 16