Methods in Java

In this tutorial, we will learn about Methods in Java. A method is a small piece(block) of code which can perform some special task and it only executes when we call this method. The method allows us to reuse code. Here we will learn how to create a Method and How to call a method in Java and types of Methods in Java:

Info! We send data to Method to perform some action on data. This data is known as parameters.

Defining a Method: 

Here we will learn about how we can create a Method in our program. To define a method, first of all, you have to mention Access Specifier for a method then return type of method and method name followed by () and after that, we will define the body of method in {} brackets. Check Syntax to define the method.

Access_Specifier  Retrun_Type Method_Name(){
         // Body of Method
     }

As you can see here. This is the syntax to define the method in Java. Here we will see an example in which we will create a hello() method in our program.

private void hello(){
   System.out.println("Hello World");
}

Calling a Method:

After creating a method in the program we can call method. Here we will call the method from our main method. Before calling method we have to create an object of our class. Then we will call the method using this object. Do not worry if you don’t know what is this object and how it works we will learn OOPs concept in upcoming tutorials there you can learn objects in detail.

class MethodExample{
   public static void main(String args[]){
      MethodExample obj=new MethodExample();
      obj.hello();
      obj.hello();
   }
   private void hello(){
   System.out.println("Hello World");
   }
}

Types of Methods in Java:

As we discussed in starting of the tutorial. There are four types of method in Java.

  • Method without parameter and without a return statement.
  • Method without parameter and with a return statement.
  • The method with parameter and without a return statement.
  • The method with parameter and with a return statement.

Here we will check each type in detail with the help of examples.

Method without parameter and without return statement:

In this kind of method return, statement and parameter would not present in the method definition. In the last example, we have checked this kind of method.

Method without parameter and with return statement:

In this kind of parameter, there would not parameter in method definition but there would return statement with the help of this return statement we can send some return value back to the caller of the method. Check this example.

class MethodExample
   {
    public static void main(String[] args){
      int result =  new MethodExample().sum();
      System.out.println("Result is = "+result);
     }

     private int sum(){
       int a=10,b=20;
      return a+b;
     }
}

Method with parameter and without return statement:

In this kind of there return statement would not present in the method definition. But there would parameters which you have to pass while calling that method. Check this example.

class MethodExample
   {
    public static void main(String[] args){
      new MethodExample().sum(10,15);
     }

     private void sum(int a,int b){
      System.out.println("Result is = "+(a+b));
     }
}

Method with parameter and with return statement:

In this type of method, both the return statement and parameters would present in the method definition. Check this example.

class MethodExample
   {
    public static void main(String[] args){
      int result =  new MethodExample().sum(10,15);
      System.out.println("Result is = "+result);
     }

     private int sum(int a,int b){
      return a+b;
     }
}

 

Spread the love
Scroll to Top
×