Method Declaration and Method Signature

Method Declaration and Method Signature

In Java programming, programmer-generated methods are individual blocks of program source code that perform a specific programming task, and are a necessary part of a program in Java. By understanding method declaration and method signature in Java, you can easily deal with Java function tasks and programs. Where Java programmers can decide how to define methods in a function in Java, how to use a function method, and how to call an actual function argument with a formal parameter.

Method Declaration and Method Signature

Method declaration in Java programming.

Method declaration in Java program is a basic process of defining a function method in Java. Where the function method declaration indicates the function name, function parameters, function return type, and the specific portion of the function method.

Basic syntax of method declaration in Java programming.

<access modifier> <return type> <method name>(<parameter list>) {

    // custom programmer created Method body

    // write method Code to be executed

}

  • Method access modifier – Here method indicates visibility, where in the function method type, public, private, protected, method options are there. Here if a function method does not have any modifier indicator, then it works on package-private by default.
  • Method return type – The data type variable value returned by the function method, such as int, string, void, has a default return of 0. If the function method does not return any value here, then you use void data type return in it.
  • Function method name – This is the name of the function declare method in the current Java program. Remember, it should properly follow the variable naming rules and regulations of Java.
  • Method parameter list – There is a list of function method parameter arguments in the Java program, which the function method will accept. Here method parameter declaration is optional, if the method does not accept function parameters, then the method parameter brackets will be empty. For example, ().
  • Method body – It defines that portion of the source code in the Java program. For example, what is the method’s work in the current function. Where the body is combined in curly braces { }.

Function method declaration example in Java.

public class Main

{

            // let declare function Method with no parameters and returns nothing value

    public static void vcanhelpsu() {

        System.out.println(“\n welcome to vcanhelpsu.com!”);

    }

    // let declare Method with take two integer parameters and returns an integer output

    public static int totalvalue(int p, int q) {

        return p + q;

    }

    // let declare Main method which call above declare other methods

    public static void main(String[] args) {

        vcanhelpsu(); // it alling message from vcanhelpsu

        int output = totalvalue (1, 2); // it Calling the totalvalue function method

        System.out.println(“\n The total is – ” + output);

    }

}

Function method declaration explanation in Java.

vcanhelpsu() is such a function method. Which does not accept any parameter argument in the current program, and does not return any program value to the user. So here the return type of this function is void return type.

totalvalue(int p, int q) is such a Java function method. Which accepts two integer parameter variable value input from the Java programmer, and its default return type is an integer value.

Where above in this program method with method parameter and without function parameter method declaration example is shown.

Method signature in Java programming.

Method signature in Java program is a unique declaration identification indication of the method declared inside a class. It includes the name of the current program method and function parameter list type and order of parameter arguments. But the existing function return type or function access modifiers are not included.

In simple language, Java method signature indicates how the method in Java can be declared and used uniquely from other program methods.

Components of method signature in Java programming.

  • Function method name – This is the name of the method declared in the Java program.
  • Function method parameter list – This is the type and order (not parameter names) of the parameter function arguments in the existing Java program.

Function method key points.

Remember, in Java program the function return type is not considered as a portion part of the method signature.

Java access modifiers like public, private does not form part of the method signature.

Remember, if two methods in a Java program have the same name but both methods have different argument parameter types or variable order. Then both those methods have their own different method signatures. This method provides overloading permission in Java program.

Examples of method signatures in Java programs.

public class MethodSignature {

    // let declare Method with signature: “totalvalue (int, int)”

    public int totalvalue(int p, int q) {

        return p + q;

    }

    // let declare Method with signature: “totalvalue (double, double)”

    public double totalvalue(double p, double q) {

        return p + q;

    }

    // let create Method with signature: “displayMessage(String)”

    public void printinfo(String info) {

        System.out.println(info);

    }

    public static void MethodSignature(String[] args) {

         MethodSignature obj = new MethodSignature();

        // it Call overloaded function methods

        System.out.println(obj.totalvalue(2, 9));        // it Calls the totalvalue int method version

        System.out.println(obj.totalvalue(1.4, 7.9));    // it Calls the totalvalue double method version

        obj.printinfo(“\n it display function signature method”);

    }

}

Method signature explanation in Java programs.

Here in Java program method totalvalue(int, int) data type signature is almost multiple of totalvalue(double, double) because here data type parameter types are different.

Where as printinfo(String) method has a unique signature in the program because it accepts a string parameter argument.

Where as in current program method signature of return type(int, double, void) is missing in Java program.

Overloading methods based on signatures in Java programs.

Java programming provides you method overloading permission. Which means you can hold multiple methods in same classes with same name in current program. But here both the declared class methods can have multiple signatures. Remember here method signatures should be different according to argument parameter numbers or data type.

Example of method overloading in Java.

public class MethodOverloading {

    // let declare function Method only one parameter

    public void output(int p) {

        System.out.println(“\n the Integer value is – ” + p);

    }

    // let delare function Method only one parameter with different type

    public void output(String info) {

        System.out.println(“\n the String is – ” + info);

    }

    // Method with two parameters

    public void output(int p, String info) {

        System.out.println(“\n the Integer – ” + p + “, the String is – ” + info);

    }

    public static void main(String[] args) {

        MethodOverloading obj = new MethodOverloading();

        obj.output(1);                // it Calls output int p method

        obj.output(“welcome,to vcanhelpsu!”);  // it Calls output String info

        obj.output(4, “method overloading”);       // it Calls output the int p, String info

    }

}

Method overloading explanation in Java.

The method display(int number) in Java is overloaded by creating two other versions of display, one which takes a String parameter argument, and the other which takes both int data type and String data type as separate arguments input from the user.

Here the Java compiler selects the correct method based on the arguments passed during the method call process.

Method Signature vs. Method Declaration

AspectMethod DeclarationMethod Signature
DefinitionThis is the complete definition of a function method, including its return type, method name, method parameters, and method body.It is the unique identifier of a method name, which consists of the method name and method parameter list (but there is not the return type for different method).
IncludesMethod Return type, declare method name, parameter list, and method body portion.It includes Only method name and method parameter list (order and types).
Used ForIt used to Declaring the method in the code.It used for distinguishing different methods (used in method overloading process).
ExampleMethod example – public int total(int p, int q) { return p + q; }It total(int, int)