Method Signature in Java with example

In this article, we are going to learn about the method Signature in Java. As we all know, Java is used for various applications due to its vast number of flexible modules, let us dive deep into one of its interesting features.

Definition – A method signature refers to writing the method name with its parameters without the return type of the method.

Passing parameters in Java is an essential chore for programmers.

For example, the Java method signature of the below example is:-  method_name(int, int).

public class sampleclass{
      public void method_name(int a,int b)
           {
            //method_definition
            }
}

If we declare a different parameter for the same method, its method signature changes.
for example, for the below code, the method signature is:- method_name(double).

public class sampleclass{ 
     public void method_name(double a) 
           { 
             //method_definition
           }
}

Since the method signature does not allow return type, class methods can be overloaded. Method overloading includes no return type and only the type of arguments, method signature eliminates the error of different return types for the same function and support method overloading.

For example,  in the below code, the function sample() is overloaded due to different parameters.

public class sampleclass{
     public void sample()
        {
           //method definition
        }
     public int sample(int a)
        {
           //method definition
        }
}

In the above code, since the method signature does not include return type, i.e, void, and int, function overloading in Java can be achieved based on the types of parameters. The method signatures of sample() are sample() and sample(int).

I hope this article is helpful, please put a comment in case of any query.

Leave a Reply