by Dinesh Thakur Category: Java Methods

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.

Defining a method within a class.

public class SumOfNum


   static int Calculate()


        int total = 0;

        int marks[] = {80,65,73,92,67};

        for (int i=0;i<5; i++)

              total += marks[i];

             return total;


       public static void main(String args[])


             int Sum = Calculate();

             System.out.println("Total marks = :" +Sum);



The output of the SumOfNum program is as follows:

Total marks =: 377

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.