Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to create a method in Java

Tarun Telang


Methods are the basic building blocks of any Java program. A method is simply a named block of code that can be executed.

Methods are essential because they allow us to break our program into smaller, more manageable chunks. This makes our code easier to read and understand.

Creating a method in Java is a two-step process.

  1. We'll create the method signature, which includes the method name, the return type, and parameters.
  2. Then, we'll write the method body, which contains the code executed when the method is called.


To create a method signature, we'll use the following syntax:

public <return type> <method name>(<parameters>)
Syntax of a method signature


For example, the following code creates a method that takes two integers as parameters and returns an integer:

class MyClass {
  public int add(int a, int b) {
    // code for the method body 
    int result = 0; 	
    result = a + b;
    return result;

  public static void main( String args[] ) {
      MyClass instance = new MyClass();
      int result = instance.add(10, 5); 
Example for creating a method

Defining methods in a class

The first line of the code is the method signature. The keyword public indicates that the method is called from outside of the class in the program. The return type is int, which means that the method will return an integer value. The method name is add, and the two parameters are int a and int b.

The method body is the code that will be executed when the method is called. In the example above, the code in the method body simply adds the two parameters together and returns the result.

Invoking a method from an object

We can call a method from anywhere in our program using the following syntax:

<object reference>.<method name>(<parameters>)
Syntax for calling a method

For example, to call the add method with the parameters 5 and 10, we use the following code:

int result = myClass.add(5, 10);

This would set the result variable to 15.

We can also write a method that does not return any value. For example, the following code creates a method that prints a message to the screen:

public void printMessage() {
	System.out.println("Hello, world!");
A method without a return type

This method doesn't have a return type, so the keyword void is used in the method signature.

To call this method, we use the following code:

<object reference>.printMessage();
Call a method without parameters

This would print the message "Hello, world!" to the screen.

class MyClass {
	public void printMessage() { 		
		System.out.println("Hello, world!"); 

public class Main {

  public static void main(String[] args) {
    MyClass object = new MyClass();
    object.printMessage(); // prints "Hello, world!" to the screen
Example for a method without parameters

In the code above, we first create a class called MyClass. This class has a method called printMessage that prints a message to the screen.

Next, we create an instance of MyClass and call the printMessage method on it. This will cause the message "Hello, world!" to be printed to the screen.


In this answer, we learned how to create a method in Java. We also learned how to call a method from another class. Methods are an important part of any Java program.



View all Courses

Keep Exploring