Methods in java

Let’s learn methods in java.

methods in java

Methods in java

Java method is a group of statements which executes some logic and returns result to the caller. Methods can be used to reuse code.

Also read – classes and objects in java

Method is declared inside a class and is referred by name and called or invoked in java program using method name. Now let’s see how to write a method in java,

For example: System.out.println() method

Java method syntax:

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

public int add(int a, int b)
{
   return a + b;
}


In the above method syntax java,

  • “public” is an access modifier. By declaring public it can be accessed anywhere in an application.
  • “int” is integer return type to return some value. If it is void, it does not return any value.
  • “add” is name of the method.
  • method body is enclosed between two curly braces.
  • add() method has two parameters ‘a’ and ‘b’. They can be left empty or you can pass parameters. Now let’s see an example,
class Demo
{
   public static int sum(int x, int y)
   {
      return x + y;
   }
}


method “sum” has two parameters “int x” and “int y” which returns sum of x and y.


Naming a method in java

Name of a method should be a verb (lowercase) followed by noun or adjective.

NOTE: after first word in method first letter should be capitalized.

For example calculateAverage, findAverage, findArea…etc.


calling methods in java

To call a method in java, first write method name along with parentheses () and semicolon. In the below example we are calling method printHello() inside main() method.

class Demo 
{
   static void printHello() 
   {
      System.out.println("Hello World");
   }

   public static void main(String[] args)
   {
      System.out.println("In main() method");
      printHello();
   }
}



Output:

In main() method

Hello World


Let’s see another example where method is non-static,

class Demo
{
   public static void main(String[] args)
   {
      A obj = new A();
      System.out.println("In main() method");
      obj.printHello();
   }
}

class A
{
   public void printHello()
   {
      System.out.println("from non-static method printHello()");
   }
}



Output:

In main() method

from non-static method printHello()


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

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


Java method with parameters or arguments

In java, a method can have arguments or with no arguments. To specify parameters we have to first write method name and then inside parentheses we can add parameters separated by comma(,).

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

class Demo 
{
   static void printName(String firstName) 
   {
      System.out.println(firstName + " Shastri");
   }

   public static void main(String[] args) 
   {
      printName("Ravi");
      printName("Shyam");
      printName("Shiv");
   }
}



Output:

Ravi Shastri
Shyam Shastri
Shiv Shastri


Java method returning value

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

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 of a java program,

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 methods

  • code readability and reusability. Method written once is used several times in a java program.
  • If there is an error in a method it will be easier to debug the code. Since a method would be written seperately in a java program.