Method in Java With Examples

A method in Java is a block of code that performs a specific task. It is similar to a function in other programming languages. Methods are an essential part of object-oriented programming, as they allow us to write code that is more modular, efficient, and reusable. In this tutorial, we will delve into the details of methods in Java and provide examples to illustrate their usage.


Methods are an important part of object-oriented programming, as they allows us to encapsulate code and hide its implementation details from other parts of the program. This helps to make the code more modular, efficient, and reusable. Methods can accept input in the form of parameters and they can return output in the form of a return value.

1. Declaring a Method in Java:

To declare a method in Java, we need to specify the following elements:


1. The method's return type: This can be any primitive data type (such as int, double, or boolean) or a reference type (such as String or Object). If the method does not return a value, the return type should be void.


2. The method's name: This should be a descriptive name that reflects the task that the method performs. It should follow the same naming conventions as variables in Java (e.g., it should begin with a lowercase letter and use camelCase).


3. The method's parameters: These are the input values that the method accepts. Each parameter has a data type and a name, and multiple parameters are separated by commas.


4. The method's body: This is the code that defines the task that the method performs. It is enclosed in curly braces.


Here is an example of a method declaration in Java:

java
public int addNumbers(int x, int y) {
  int sum = x + y;
  return sum;
}

In this example, the method is named addNumbers and it returns an int value. It has two int parameters named x and y, and it calculates their sum and returns it.


2. Calling a Method in Java:

To call a method in Java, we need to use its name followed by a set of parentheses. We can also pass in any required parameters within the parentheses. For example:

java
int result = addNumbers(3, 4);

In this example, the addNumbers method is called with the values 3 and 4 as parameters. The method executes and returns the value 7, which is then stored in the result variable.


3. Using Return Values from Methods:

If a method returns a value, we can use that value in our code by assigning it to a variable or using it in an expression. For example:

java
int result = addNumbers(3, 4);
int doubled = result * 2;
System.out.println(doubled); // prints 14

In this example, the addNumbers method is called and the return value is stored in the result variable. The result variable is then multiplied by 2 and stored in the doubled variable. Finally, the value of doubled is printed to the console using the println method of the System.out object.


4. Using void Methods in Java:

If a method has a return type of void, it does not return a value. We can still call a void method and execute its code, but we cannot use the return value in our code. For example:

java
public void printMessage(String message) {
  System.out.println(message);
}

...

printMessage("Hello, World!");

In this example, the printMessage method is a void method that accepts a String parameter and prints it to the console using the println method. The method is called with the message "Hello, World!" and the message is printed to the console.


5. Conclusion:

Methods are an essential part of programming in Java, and they allows us to write modular, efficient, and reusable code. By understanding how to create, call, and use methods in the code, we can greatly improve the structure and maintainability of the programs.