Methods in Java

BerkayHasip
3 min readJan 28, 2024

--

What is a method?

A method is a block of code that performs a specific task. For example, you can create a method to draw a circle, or a method to add two numbers.

Methods are useful because they allow you to reuse code without writing it again and again. They also make your program easier to understand and maintain.

How to create a method?

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

  1. Choose a name for your method. The name should be descriptive and follow the camelCase convention. For example, drawCircle or addNumbers.
  2. Specify the return type of your method. The return type is the type of value that your method gives back to the caller. For example, if your method returns an integer value, the return type is int. If your method does not return anything, the return type is void.
  3. Write the parameters of your method. Parameters are values that you pass to the method when you call it. They are used to customize the behavior of the method. For example, if your method draws a circle, you can pass the radius and the color as parameters. You can have any number of parameters, or none at all.
  4. Write the body of your method. The body is the code that performs the task of the method. It is enclosed in curly braces { }. You can use variables, expressions, statements, and other methods inside the body.
  5. End the method declaration with a semicolon ;.

The general syntax of a method declaration is:

Java

returnType methodName(parameter1, parameter2, ...) {
// method body
}

Here is an example of a method that adds two numbers and returns the sum:

Java

int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}

How to call a method?

To use a method in your program, you need to call it. To call a method, you write the name of the method followed by parentheses () and a semicolon ;. If the method has parameters, you need to pass the values of the parameters inside the parentheses. If the method has a return type other than void, you need to store the returned value in a variable.

For example, to call the addNumbers method, you can write:

Java

int result = addNumbers(10, 20); // call the method and store the returned value in result
System.out.println(result); // print the result

This will output:

30

You can also call a method multiple times with different values of the parameters. For example:

Java

int result1 = addNumbers(5, 15); // call the method with 5 and 15
int result2 = addNumbers(20, 25); // call the method with 20 and 25
System.out.println(result1); // print the first result
System.out.println(result2); // print the second result

This will output:

20
45

Summary

In this article, you learned:

  • What is a method and why it is useful
  • How to create a method with a name, a return type, parameters, and a body
  • How to call a method with or without parameters and return values

--

--

BerkayHasip
BerkayHasip

Written by BerkayHasip

Website : berkayhasip.com / Hi! Student , Java lover. Self-taught, blog my journey. Create apps, freelance, advise, and blog. Eager for diverse projects

No responses yet