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 value to the caller. Methods can be used to reuse code.

Also read – Difference between == and equals method 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 static int firstMethod(int x, int y)
{
 
}


In the above method syntax java,

  • “public static” is access modifier. public static can be private or private static or protected.

Click here to learn static keyword

  • “int” return type returns integer.
  • “firstMethod” is name of the method and
  • “int x” and “int y” are parameters. They can be left empty or you can pass parameters. Now let’s see an example,
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

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.


How to call a method in java

To call a method, write method name with parentheses () and semicolon. Let’s see method example java,

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. Let’s see java methods 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


Now let’s see another java program to access methods in different ways,

class Demo 
{
   public static int num = 0; 
   // constructor which counts number of the objects 
   Demo() 
   {
      num++; 
   }
   // static method to access static members 
   // and getting total number of objects
   public static int get() 
   {
      return num; 
   }
   // instance method calling object
   // can be called by object directly
   public int print() 
   { 
      System.out.println("inside print method");
      // calling display() method within the same class. 
      this.display();
      return 65; 
   }
   // 'void' doesn't return anything
   public void display() 
   {
      System.out.println("inside method display from method print"); 
   }
}


public class FB 
{
   public static void main(String[] args) 
   {
      // creating instance of the class 
      Demo obj = new Demo();
      // calling print() method 
      int a = obj.print(); 
      System.out.println("return after method print : " + a);
      int number = Demo.get();
      System.out.print("number of instances created : "); 
      System.out.println(number);
   }
}


Output:

inside print method
inside method display from method print
return after method print : 65
number of instances created : 1


Java method parameters

We can pass parameters to method. Like sort of sending information or data to method.

To specify parameters we have to first write method name and then inside parentheses we can add parameters separated by comma(,). Let’s see java program,

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