Java

Methods in Java

A java method is also known as a function and it can be either predefined or user-defined. Both types of methods are used to perform different functionalities, like calculations,  etc. The major difference between both these method types is that a predefined method is already defined method in any programming language and ready to use anywhere in the program while a user-defined method is defined by the user/programmer as and when required. This write-up will provide a detailed understanding of the following concepts regarding Java Methods.

  • What is a Java Method
  • Basic Syntax of Java Method
  • How to Create and Call a Method
  • How to Pass Parameter(s) to a Method

So, let’s start!

Java Method/Function

In Java, a method is nothing but a unit or a block of code that is used to perform a specific task/operation. A Java method runs only when we call it and once a method is defined in a program, it can be used anywhere within the scope of the method.

Syntax of Java Method

The below-given snippet provides the basic syntax of the user-defined Java method:

returnType methodName(parameters)
{
//statements
}

A Java method can have a returnType, methodName, and parameter list.

  • The returnType of the method can be void, a primitive type such as int, float, etc. or a reference type
  • while a methodName can be any name written in camel case naming convention, followed by the parenthesis ().
  • Within the parenthesis(), a single parameter or a list of parameters may or may not be passed.

How to Create and Call a Method

In Java, a method must be created within the class, and to do so all we have to do is define the function/method name followed by small brackets (). Let’s consider an example for a better understanding of how to create and call a user-defined method in java.

Example
This example will take two values from the user and add them using a user-defined method “sum”:

public class Addition {
 static void sum(){
 int num1, num2, result;
 Scanner  scan = new Scanner(System.in);
 System.out.println("Enter First Number");
 num1 = scan.nextInt();
 System.out.println("Enter Second Number");
 num2 = scan.nextInt();
 result= num1+num2;
 System.out.println("Sum of two Numbers : " + result);
}

Now the method is successfully created and it’s time to call the method and to do so we will write the method’s name followed by parenthesis in the main method:

public static void main(String[] args) {
 sum();
}

The below-given figure provides a complete code and output:

The above-snippet authenticates when we call the sum() function from the main method then it provides the sum of user-entered values.

How to Pass Parameter(s) to a Method

The value(s) can be passed to a method through the parameter(s) and these parameters serve as variables within a method. We have to pass the values as arguments when calling the method.

A parameterized method can have single or multiple parameters and all the parameters will be written within the parenthesis. In the case of multiple parameters, all the parameters must be separated with a comma.

The below-given example will provide a better understanding of how to pass the parameters to a method.

Example
This example will provide the sum of user-entered data and the data will be passed as parameters:

We create a function to add two numbers:

static void addition(int n1, int n2){
int result;
result = n1 + n2;
System.out.println("Sum : "+ result);
}

In the main method we take two values from the user:

public static void main(String[] args) {
 int number1, number2;
 Scanner  scan = new Scanner(System.in);
 System.out.println("Enter first Number");
 number1 = scan.nextInt();
 System.out.println("Enter second Number");
 number2 = scan.nextInt();

 addition(number1, number2);
}

And we passed the user-entered data as parameters to the sum function and call it from the main method.

The complete code and the respective output is shown in the below-given figure:

Let’s conclude how the above program works, two values are taken from the user and passed them as arguments to the addition method. The addition method adds both the values and displays the result.

Conclusion

In java, a user-defined method can be either a parameterized or a non-parameterized method. The non-parameterized methods don’t take any data as parameters. On the other hand, the parameterized method takes a parameter or a list of the parameters that receive the respective values from the method calling. This write-up presents a comprehensive overview of what is java method, and how to create and call a method. Moreover, it provides a complete guide for the parameterized Java methods.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.