Method in Java3 min read

Java Method is a collection of statements that are put together to perform some specific task. To run the methods, it should be called and it returns the value to the caller. Users can pass data to the methods and those data are known as parameters. They are also known as a function.

It allows the user to reuse the code as many times without rewriting the code and therefore act as a time saver.

Syntax of method:

Explanation:

Modifier:

It is optional and defines the access type of a method. In the above example, the ‘public’ is the modifier of the method calculates.

Return Type:

The return type is for returning the value by defining the data-type of the method or void is used if the method does not return a value. In the above example, ‘int’ is the data type for the return type.

MethodName:

It is the name given to the method to identify the specific method. ‘calculate is the method name in the above example.

Parameter List:

These are the input parameters with their specific data types. The passed parameter data type must match with the parameter list of a method and also the order in which it is passed and must be separated by commas(,). If there are no parameters, the user needs to use an empty parentheses ‘()’. ‘int x, int y’ is the parameter list in the above example.

Method body:

The method body is enclosed within the curly braces'{}’. It defines the task of a method within the curly braces.


Call A Method:

To use the method and its functionality in a program it must be called. To call a method, the user needs to write the method name followed by two parentheses ‘()’ and a semicolon ‘;’. When a program calls a method, the control of a program remains with the method and then again return the control to the caller in two conditions:

  1. If completes all the statements within the curly braces ‘{}’.
  2. If the return statement is executed.

Basic example to call a method more than once with empty parameter List in Java:

Output:


Void Keyword:

This Keyword allows us to create methods that do not return any values.

Basic example to find the max number with method parameter and with return type in Java:

Output:


Method Types

There are two types of method:

  • Standard Library Methods
  • User-defined Methods: In the above part we lean about the User-defined methods.

Standard Library Methods:

These methods are built-in methods in Java that are readily available for use. These standard libraries come along with the Java Class Library (JCL) in a Java archive (*.jar) file with JVM and JRE.

Some example of Standard Library Methods:

  • print() -use to print the String.  And it comes under java.io.PrintSteam
  • sqrt() -use to find the square root of the number and it is a method of Math class

MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …