The Best Technical Blog Ever


Methods in java

A method is a group of Java statements that perform some operations on data by calling (also called invoking) it from some other place in your program. A Class contains two elements, (1) data members (2) methods. Methods are mainly used to break down the larger code into smaller, more comprehensible and reusable segments of code. Methods are known as functions in C and C++ programming languages. A method has a name and return type. It must be located inside a java class. Every program must have at least one method for the program to accomplish any work. The main method is a must in a Java program as execution begins from it.

Defining Methods:

The required elements for defining methods are the method's return type, name, a pair of parentheses, (), and a body between braces, {}. Commonly method definition has six components. They are,

  • Access Modifier:

    such public or private which decides whether other classes can call a method. It is optional to use.

  • static:

    This optional keyword declares that the method is a static method, which means that you can call it without first creating an instance of the class in which it's defined. The main method must always be static.

  • The return type:

    the data type of the return value which method returns, or void if the method does not return any value.

    public int add(int a, int b) {
         return a +b;

    This method adds the two parameters passed to it, and returns the result. The return type "int" signals that this method returns an int value. You can return any primitive type or any object from a Java method.

  • The method name:

    valid Java identifier name.

  • The parameter list in parenthesis:

    a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). You can pass one or more values to a method by listing the values in parentheses following the method name. The parameter list in the method declaration lets Java know what types of parameters a method should expect to receive and provides names so that the statements in the method's body can access the parameters as local variables. These are optional, method may contain zero parameters. If there are no parameters, you must use empty parentheses.

  • The method body, enclosed between braces:

    the method's code defines what the method does with these statements. It includes the declaration of local variables. Unlike Java statements such as if,while, and for, the method body requires you to use the braces even if the body consists of only one statement.

    accessmodifier returnType methodName(parameter List) {
         // body of method

    The example above defines a method called add. This method takes two parameters a and b. These parameters are used in the Java statements inside the method for doing calculations and finally returns the value c .

Method Calling:

When a program calls a method, the program control gets transferred to the called method. After executing this method, program returns control to the caller in two conditions,(1) return statement is executed. (2) reaches the method's closing brace.


Actual and Formal Parameters:

  • Formal parameter- is an identifier used in a method definition to stand for the value that is passed into the method by a caller.
  • Actual parameter- is the actual value that is passed into the method by a caller. Actual parameters are often called arguments

The names of the actual and formal parameters are unrelated. So formal and actual parameters need not be on same name. But they must be in same data type.


Parameter passing in java:

In Java, all parameters are passed by value. Java only supports pass by value. For java objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same Java object. Pass-by-value defines that when a method is invoked, a copy of the value of each actual paarameter/argument is passed to the method. This copy can be changed inside the method, but such a change will have NO effect on the actual parameter until no return statement is used.


Note: Copies of the actual parameter values from main are sent to the method, where they become the values for the formal parameters. When the method is finished executing, the copies are discarded. The actual parameter values remain unchanged. (Notice that nothing was "returned" from this method.)

Passing Objects to Methods:

Up to this point, the examples pass everything by value as parameters to methods. However, it is possible to pass objects to methods. The following program demonstrates how to pass object as parameter.


Return Statement:

A method can return a value. If a method doesn't return any value, then void keyword is included before the method name. On the other hand to return a value, the void keyword should be replaced with the data type of the return value which method return and the return keyword is added to the method body with an argument of the specified return type.

String Display() 
 	 return "Hello";

Return also act as a jump statement that causes the method to exit and return the specified value to the place where the method was called. Thus, the return statement may also be used in void methods to exit before the end block is reached.

Using Command-Line Arguments:

In run time, we can pass information to the program by passing command line arguments to main() method.


The data type of the parameter variable args of the main method is an array of String. args[0] is the first element of this array. args[1] is the second element of this array. When we execute this program using the following syntax:


Here args.length is 3.

Exception Declarations:

If an error occurs inside a method, the method may throw an exception. Following example shows the exception declaration. Exceptions are discussed in detail in a later lesson.

public int max(int a,int b) throws MyException {
       //method body


In Java, a method can call itself. This process is called recursion, and a method that calls itself is said to be recursive. The key component of a recursive method is a statement that executes a method call to itself. Recursion is a powerful control mechanism. For that, you need to define a ending point. Then only it will work properly. If you fail to give the ending point, you might get caught in an infinite loop and you'll have to manually terminate your program. The best example of recursion is the computation of the factorial of a number.


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status