In the Java method, the signature consists of the method name followed by argument types.
method(int, float)
The return type of the method is not a part of the method signature in Java.
The compiler will use the method signature to resolve method calls.
public class MethodSignature {
public void method(){
System.out.println("Method with one argument");
}
public void method(int a, int b){
System.out.println("Method with two argument");
}
public static void main(String[] args) {
MethodOverloading object = new MethodOverloading();
object.method();
object.method(1,2);
}
}
In a class, more than one method with the same signature is not allowed. if we declare more than one method with the same signature then we will get compile time error- “‘method(int)’ is already defined in ‘MethodSignature'”.
public class MethodSignature {
public void method(int a){
}
public void method(int a){
}
}
Similar Java Tutorials
- Odd Even in Java
- Operators in Java
- Public static void main
- Command line argument in Java
- Java coding standards
- Do while loop in Java
- Import statement in Java
- Abstract modifier in Java
- Strictfp modifier in Java
- Final variable in Java
- Static modifier in Java
- Native modifier in Java
- Transient keyword in Java
- Volatile keyword in Java
- Adapter Class in Java
8 thoughts on “Method Signature in Java”