A Java method contains a block of statements/instructions that perform some functionalities only when someone calls the method. When someone calls a java method, multiple statements executes at the backend to provide a certain output. The Java methods provide the reusability of the code, as we have to write/create a method once, and we can use it as many times as we want.

Java provides two types of methods i.e the predefined methods and user-defined methods. This write-up will provide a detailed understanding of user-defined methods. Following concepts of user-defined Java methods will be elaborated in this tutorial:

So, let’s start!

What is Java Method

A Java method is a block of code/statements which may or may not take some input and returns some output. A method must be declared within the class. To create a Java method we have to follow a proper syntax as described below.

Basic Syntax of Java Methods

The below-given snippet shows how to declare a method in Java:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/1-14.jpg" data-lazy- height="146" src="data:image/svg xml,” width=”605″>

A modifier/access specifier specifies the access type of a method, and java provides four types of modifiers i.e. default, public, private, and protected.

  • The public modifier specifies that the method is accessible to all classes/child classes,
  • The private modifier specifies that the method is accessible to only those classes in which it is specified,
  • The protected modifier specifies that the method is accessible within the specified package only.

In Java, there are multiple keywords that have some special meanings, in the above snippet static keyword is used to define that the method can access the static data.

Return type specifies which type of data will be returned by the method in the above snippet void is used which represents no data type will be returned.

Lastly, Method’s name is the name of the method using which we can call it.

Creating Method in Java

To create a user-defined method, we have to specify a method name. The method name follows the camel casing naming convention and starts with the small letter or if you want to specify a multi-word method name then the first letter of every word will be a capital letter except the first letter.

Let’s consider the below-given example to understand how to create a method in Java:

Example

The below-snippet creates a method to find a square of a number:

public class HelloWorld {

  static void findSquare(){

  int num, sqr;

  Scanner scan = new Scanner(System.in);

  System.out.print(“Enter a Number: “);

  num = scan.nextInt();

  sqr = num * num;

  System.out.println(“Square of  “ num ”  is :  “ sqr);

 }

Within the class “HelloWorld”, we created a method findSquare(). Scanner class is used to take the input from the user moreover functionality to find the square of the number is defined in the findSquare() method.

How to Call a Method in Java

Once a method is created we can call it and to do so, we have to write the method name followed by () as shown below:

public static void main(String[] args) {

 findSquare();

}

The complete code and its output is shown in the below-given snippet:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/2-14.jpg" data-lazy- height="505" src="data:image/svg xml,” width=”692″>

User entered a number “12” and as a result gets the square as “144”


.

Conclusion

To create a method we have to specify access modifier, and return type followed by the method name, all the functionality will be defined within the method. In order to call a method, we have to specify a method name followed by parentheses (). This write-up demonstrated how to create and call a method in Java with the help of a simple and to-the-point example.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/FA9AFCF66E2D44C3A65378AC87A87EB4-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.