JAVA METHOD SIGNATURE

In Java , Method Signature is basically the combination of number and types of arguments. It is a part of Method Prototype. Sometimes Java Method Signature is used to refer to the method Prototype.

Table of Contents

[hide]
  • What is Java Method Signature
  • Example :Java Method Signature

What is Java Method Signature

Before diving into the detail of Java Method Signature, let us learn about Java Method Prototype.

Java Method Prototype

A Method Prototype is the first line of the method definition that informs the program about the type of the value returned by the method and the number and type of arguments.

Using method Prototype, the compiler compares the number and types of arguments at compile-time and any reported incorrect number or type of arguments helps the compiler to easily point out an error.

Example : public static int minimum(int a, int b)

In the above example return type is int and the number of arguments paased are two. Types of arguments are of int data type.

In the above example combination of name of method, number and types of arguments can be referred to as Java Method Signature. While combining them with the return type make them as method Prototype.

The explanation for concentrating on the name of the method and the list of parameters is because of overloading. This is the ability to write methods that have the same name but accept different parameters. The Java compiler is able to distinguish the difference between the methods by means of their method signature.

Example : int minimum(int a, int b);

Java Method Signature

Now let us see an example of the following example in the next module.

Example : Java Method Signature

Let us use the method defined above to get the better understanding of method signature.

Example : int minimum(int a, int b);

copyimage
//Java Program to demonstrate the use of Java Method Signature
import java.util.*;
public class MethodSignature {
     public static void main(String args[]){  
     System.out.println("Enter two numbers");
     Scanner sc = new Scanner(System.in);
     int a = sc.nextInt();
     int b = sc.nextInt();
     int y = minimum(a,b);// Java Method Signature
     System.out.println("The minimum one is"+ " "+ y);
 }
     public static int minimum(int  x, int y){ 
     int result;
     result = x < y ? x : y;
     return result;
   }
}

OUTPUT :

 Enter two numbers4455The minimum one is 44

NOTE : We are accessing the Java Method without using an object beacause of the static variabe being used in the method.

static means that the current method is a class method, it will be called through class name or can be accesed directly rather than through object. If you remove static from the method, it will through you error. In that case you have to create an object.

Example : Class with two methods with same signature.

A class with two methods with same signature will give you compile time error.

copyimage
//Java Program to demonstrate the use of Java Method Signature
import java.util.*;
public class MethodSignature {
     public static void main(String args[]){  
     System.out.println("Enter two numbers");
     Scanner sc = new Scanner(System.in);
     int a = sc.nextInt();
     int b = sc.nextInt();
     int y = minimum(a,b);// Java Method Signature
     System.out.println("The minimum one is"+ " "+ y);
 }
     public static int minimum(int  x, int y){ 
     int result;
     result = x < y ? x : y;
     return result;
   }
    public static double minimum(int  x, int y){ 
     double result;
     result = x < y ? x : y;
     return result;
   }
}

OUTPUT :

ERRORRename method minimumDuplicate method minimum(int, int) in type MethodSignature

We have learned about Java Method Prototype and Java Method Signature in the given tutorial. Hope you got the concepts clear.