How to Create Method in Java?

Before understanding how to create method in Java, first let’s understand what is method in Java. Then we will learn step by step guide to adding method in a Java classs.

What is method in Java?

In Java, a method is a block of code that performs a specific task or operation. It is used to define the behavior of an object or a class. Methods are reusable, which means you can call them multiple times within your code. They encapsulate functionality, making code modular and easier to manage.

Steps to add method in Java

To add a method in Java, you need to follow these steps:

  1. Decide the class where you want to add the method: Determine the class in which you want to add the new method. It could be an existing class or a new one that you have created.
  2. Write the method signature: The method signature consists of the access modifier (e.g., public, private, protected), the return type of the method (void if the method does not return anything), the method name, and the parameters (if any) enclosed in parentheses.
  3. Define the method body: Write the code that goes inside the method to define its behavior. This is enclosed within curly braces {}.

Example of adding method in Java class

Following is a basic example of adding a method in Java:

Suppose we have a class called Calculator that performs some basic arithmetic operations, and we want to add a method called add that adds two numbers and returns the result.

public class Calculator {

    // Existing methods and properties (if any)

    // New method: add
    public int add(int num1, int num2) {
        int sum = num1 + num2;
        return sum;
    }

    // Other methods (if any)
}

In this example:

  • The class Calculator already existed.
  • We added a new method named add.
  • The method is public, meaning it can be accessed from outside the class.
  • The return type of the method is int because we want it to return the sum of two integers.
  • The method takes two parameters of type int: num1 and num2.
  • The method body calculates the sum of num1 and num2 and returns the result.

Remember to consider the appropriate access modifiers, return types, and parameter types based on the functionality and purpose of your method. Also, ensure the method name and parameters are meaningful and adhere to the Java naming conventions.

FAQs

What is a method in Java?

In Java, a method is a block of code within a class that performs a specific task or action. Methods are used to define the behavior of objects created from a class.

What is the purpose of a method’s return type?

The return type of a method specifies the type of value that the method will return when it is executed. It can be a primitive data type or an object type, or it can be void if the method doesn’t return a value.

Can a Java class have multiple methods?

Yes, a Java class can have multiple methods. These methods can have different names and perform various tasks based on the requirements of the class.