Define a method in Java

In this tutorial, we will learn about the method in Java, how to define a method which can take a parameter or not, and also in case of a return data type or not for that method. We will fully learn about it.

Components of a method include:

  • Data type returned.
  • Parameters of the method.
  • Return statement
  • The access and non-access modifiers.

The doAdd example below is a method:

We have learned about access and non-access modifiers in previous tutorial, so in this tutorial we will not mention them.


Data type returned

A method may or may not have a return value.

When they are defined with no return value, the return value of the method is void.

For example:

When they are defined with a return value, the return value of the method can be a primitive value or an object of any class. The name of the return type can be one of eight primitive types or the name of any class or interface.

Back to the example at the beginning of this tutorial, we have the doAdd() method return a primitive value and the name of its return type is int.

When a method defines the return type, when calling that method we can assign its return value to a variable with a type value as the return value of the method.

For example:

With no return value, we cannot assign it a variable, it will be compiled error if we try to do it:

Define a method in Java


Parameters of the method

Method parameters are variables declared in the definition of the method that specifies the type and value that the method can use. These parameters are declared separated by commas.

The doAdd() method in the first example of this tutorial has two parameters a and b that are declared with the same int value.

We can no pass or pass many parameters to a method and we can also declare the following:

The doAdd() method in the example above declares a parameter with three dots after the type value of the parameter. Three dots mean the variable values are either an array or a string of values separated by commas. And so we can pass many values to the method with the same type of value that we have declared.

The definition of the three dots is only true when we define this parameter at the end of the method parameter list. If we try to do otherwise, we will get the error right away:

Define a method in Java


Return statement

The return statement is used in a method, which may include a value or not.

The return statement with a value used in a method having return data type and the value follows the return statement is the return value of the method.

In the first example of the tutorial, we used the return statement with the value followed by the sum of the two numbers a and b.

With a return statement without a value, it is used to escape a method.

For example:

In this example, if the name is null, the following lines of code will not be processed because the return statement has exited the method when calling it.

We cannot put a return statement at the beginning of a method when the method does not process any statements.

The following example shows you the code that will cause compile errors if we try to do it:

Define a method in Java

Add Comment