Methods in java

Let’s learn methods in java.

Methods in java

A block of code to perform a certain operation or particular task is method. Method is declared inside a class with method name followed by parentheses ( ). A method executes only when it is called.

methods in java

We have some predefined methods in java such as sqrt(), pow() System.out.println() etc,. Meanwhile we can also create our own method. Below is syntax and example of a method.

Syntax:

access-specifier returntype methodName(list of parameters) 
{
   // method body
}

Example:

public int sum(int a, int b)
{
    // method body
}

In the above syntax,

  • access-modifier or access specifier are public, private, protected and default. If we don’t provide any access-modifier to a method in java by default then that method will have “default” access-modifier.
  • return type can be anything like int, String, or a class. If a method is not returning anything then it is a void.
  • “methodName” – As per naming convention a method name should start with a lowercase and if method name has two words then first word first character should be in lowercase and second word first character should be in uppercase.
  • Method body should be enclosed between two curly { } braces.
  • list of parameters – we can provide list of parameters like (int a, int b).

Java method example:

A method should be declared within a class. In the below example “callingMethod()” method is declared within class JavaMethodExample.

class JavaMethodExample
{
   public static void callingMethod()
   {
      
   }
}

Calling methods in java or how to call a method in java from main

In the below example we are calling static method printHello() from main() method. Here’s an example.

public class Demo
{
   static void printHello()
   {
      System.out.println("Hello World");
   }
   public static void main(String[] args)
   {
      printHello();
   }
}

Output:

Hello World


In java, a method can be called ‘n’ number of times or multiple times. Let’s see an example.

public class Demo
{
   static void printHello()
   {
      System.out.println("Hello World");
   }
   public static void main(String[] args)
   {
      printHello();
      printHello();
      printHello();
      printHello();
   }
}

Output:

Hello World
Hello World
Hello World
Hello World


Method with parameters

A method can have parameters. To specify parameters we have to first write method name and then inside parentheses we can add multiple parameters separated by comma.

Let’s see how to call a method with parameters in java.

public class Demo
{
   // here String firstName is parameter
   static void printName(String firstName)
   {
      System.out.println(firstName + " Shastri");
   }
   public static void main(String[] args)
   {
      // here Ravi, Shyam and Shiv are arguments
      printName("Ravi");
      printName("Shyam");
      printName("Shiv");
   }
}

Output:

Ravi Shastri
Shyam Shastri
Shiv Shastri


Java method: multiple parameters

We can have multiple parameters in a method. Let’s see an example.

public class MultipleParametersExample
{
   static void printName(String firstName, int ID)
   {
      System.out.println(firstName + " ID is " + ID);
   }
   public static void main(String[] args)
   {
      printName("Ravi", 1235);
      printName("Shyam", 2354);
      printName("Shiv", 1325);
   }
}

Output:

Ravi ID is 1235
Shyam ID is 2354
Shiv ID is 1325


Methods in java: Method returning value

To return value from method we can use primitive datatypes (char, int) and use “return” keyword to return value.

public class Demo
{
   static int display(int a)
   {
      return 62 + a;
   }
   public static void main(String[] args)
   {
      System.out.println(display(5));
   }
}

Output:

67


A method in java can also store output in a variable. Here’s an example.

class Demo
{
   static int multiply(int a, int b)
   {
      return a * b;
   }
   public static void main(String[] args)
   {
      int output = multiply(25, 26);
      System.out.println(output);
   }
}

Output:

650


Advantages of method

  • Code reusability.
  • Code optimization.