4.7 Basic Java Syntax: Methods

Methods in Java are blocks of code that perform a specific task and can be called from other parts of the program. They are essential for code reuse, logical organization, and modularity. In this section, we will explore method declaration, parameter usage, and the return system in Java.

Method Declaration

The declaration of a method in Java begins with an access modifier, followed by the return type, the method name, and a pair of parentheses. Inside the parentheses, we can define parameters that the method can receive. The basic syntax for declaring a method is as follows:

access_modifier return_type methodname(parameter1, parameter2, ...) {
    // Method body
}

Access modifiers determine the visibility of the method to other classes and can be public, protected, private or the default (without modifier ). The return type indicates the type of data that the method will return after execution. If the method returns nothing, we use the void keyword.

For example, a method that calculates the sum of two integers could be declared as:

public int sum(int number1, int number2) {
    return number1 + number2;
}

Parameters

Parameters are variables that receive values ​​when the method is called. They allow the method to perform operations with different inputs, making it more flexible and reusable. Each parameter is declared with a type followed by a name, and multiple parameters are separated by commas.

Let's expand the previous example to include a method that subtracts two numbers:

public int subtract(int number1, int number2) {
    return number1 - number2;
}

In the example above, number1 and number2 are parameters of the subtract method. When we call this method, we need to provide two integer arguments that will be passed to these parameters.

Return

The return value is the data that a method sends back to its caller. The return type must be compatible with the type declared in the method definition. If a method is declared with a non-void return type, then it must use the return statement to return a value. If the method is void, the return statement is optional and can be used to exit the method prematurely.

Continuing with the example above, if we want a method that prints the result of the sum instead of returning it, we could write:

public void printSum(int number1, int number2) {
    System.out.println(number1 + number2);
}

In this case, the imprimirSoma method is of type void, as it does not return any value.

Additional Considerations

In addition to the basic concepts, there are some additional characteristics of methods in Java that are important to mention:

  • Method Signature: The combination of the method name and the list of parameters is known as the method signature. This is important for method overloading, where multiple methods can have the same name but different parameters.
  • Variable Parameters: Java allows you to create methods that accept a variable number of arguments, using the three-dot syntax (...). For example: public void printNumbers(int... numbers).
  • Keyword this: Within a method, you can use this to reference the current instance of the object the method is on running.
  • Recursion: Methods in Java can call themselves, a process known as recursion. This is useful for solving problems that can be broken down into simpler subproblems of the same type.

In summary, understanding the syntax and functioning of methods is fundamental to programming in Java. Methods not only allow code reuse, but also help maintain program clarity and organization. As you progress in learning Java, you will encounter complex situations that require a deeper understanding of concepts such as method overloading, passing parameters by value and reference, and the use of methods in interfaces and abstract classes.

With practice, you will become increasingly comfortable with creating and using methods in Java, allowing you to build robust and efficient applications.

Now answer the exercise about the content:

_Which of the following statements about method declaration in Java is correct?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Basic Java Syntax: Classes and Objects

Next page of the Free Ebook:

12Basic Java Syntax: Classes and Objects

5 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text