In Java, method definition usually consists of four fundamental parts, the name of the method, the object type or the data type that the method returns (referred to as return type), the list of parameters and the body of the method.

The first three constituents of the method definition are referred to as method declaration or method signature. The method declaration usually gives the important information about the method itself.

In other programming languages, method names (function, subroutine or procedure names) are unique; however, in Java, different methods can have the same name, but a different return type or argument list.

The process of having methods with the same name but with different return type is referred to as method overloading.

In Java, a method is defined as follows:

returntype methodname (type1 arg1, type2 arg2. type3 arg3...)

{

body of the method

}

In the above illustration, returntype refers to the type of value the method returns. It can be a primitive data type, class name or void (if the method does not return a value at all). If the method returns an array object, the array brackets are placed after the return type or after the parameter list.

The two ways of returning array type in a method are depicted below:

Type I For example, the method signature

int[] method1(int a1, int a2)

{

// body of the method

}

can also be written as follows:

int method1 (int a1, int a2)[]

{

// body of the method

}

Out of these two representations, the first one is most commonly used.

The parameter list in the method definition is a set of variable declarations, separated by commas, inside parentheses (). These parameters become local variables in the body of the method, whose values are the objects or primitives passed in when the method is called.

The body of the method can consist of statements, expressions, method calls to other objects, conditionals, loops and so on.

If return type is real (that is, it is not void), a value should be returned explicitly in the body of method. Java provides the keyword return by means of which this can be done.

Program shows an example of a class that defines the Calculate method. This method calculates the total marks of five subjects stored in the array marks.