Methods in java

Let’s learn methods in java.

Methods in java

A method in java is a group of statements to carry out some operation also known as functions.

Also read – class in java

Method is declared inside a class with method name followed by parentheses ( ).

A method runs 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.

java method syntax:

modifier returnType methodName(Parameter List) 
{
   // method body
}


In the above java method syntax,

  • “public” is an access modifier. By declaring public a method can be accessed everywhere.
  • “int” is return type to return some value. If it is void, it does not return any value.
  • “addTwoNumbers” is method name. As per naming convention a method name should start with a lowercase and if method name is more than a word then every word starting should be of uppercase.
  • method body should be enclosed between two curly ({ }) braces.
  • addTwoNumbers() method has two parameters ‘a’ and ‘b’. They can be left empty.

java method example:

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

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



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

To call a method in java, first write method name along with parentheses() and semicolon.

Also read – method overloading in java

In the below example we are calling method printHello() inside main() method.

Here’s an example on how to call a method in java from main.

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


Methods in java: Method with parameters

A method can have parameters. These parameters are variables inside a method.

Also read – constructor in java

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 return 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


Use of method

  • method written once can be used several times in a program.