Skip to main content

5.2 Defining and Calling Methods

In Java, methods are essential units of functionality that define the behavior of objects. A method is a set of statements that performs a specific task, packaged together with a name. Methods allow for reusability of code and help create modular and organized programs.

Java 8 introduced various enhancements that further expanded the versatility of methods. In this section, you will learn how to define and call methods, including considerations specific to Java 8 and above.

Defining Methods​

To define a method in Java, you need to specify several key components:

  1. Access Modifier: Defines the visibility (e.g., public, private, protected).
  2. Return Type: Specifies the type of value the method returns. Use void if the method doesn't return a value.
  3. Method Name: Should follow camelCase convention.
  4. Parameter List: Defines inputs that the method can accept. Parameters are optional.

Syntax​

Here’s a basic structure for defining a method in Java:

accessModifier returnType methodName(parameterList) {
// Method body (statements)
}

Example​

public class Calculator {
public int add(int a, int b) {
return a + b;
}
}

In this example, the add method takes two integers as parameters and returns their sum.

Calling Methods​

Once a method is defined, it can be called (invoked) from another method, often the main method in the Java class or another object.

Syntax​

To call a method, specify the object (if the method is non-static), followed by a dot (.), and the method name with arguments:

objectName.methodName(arguments);

Example​

public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
int result = calculator.add(5, 10);
System.out.println("Sum: " + result);
}
}

Important Notes on Method Calls​

  • Instance Methods: Require an instance of the class (Calculator calculator = new Calculator();) to be called.
  • Static Methods: Can be called without an instance, directly by the class name (e.g., Math.sqrt(16);).

Enhancements in Java 8 and Above​

Java 8 introduced default methods and static methods in interfaces, allowing greater flexibility in designing APIs.

Default Methods​

Default methods allow interfaces to have methods with a default implementation. This feature is particularly useful in maintaining backward compatibility when adding new methods to interfaces.

interface Vehicle {
default void start() {
System.out.println("Vehicle is starting...");
}
}

Static Methods in Interfaces​

Java 8 also allows interfaces to have static methods. These methods belong to the interface itself rather than any instance of the class.

interface CalculatorUtils {
static int multiply(int a, int b) {
return a * b;
}
}

Lambda Expressions​

Java 8 introduced lambda expressions, enabling a functional programming style. They can be used for defining concise, anonymous methods especially useful for single-method operations.

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(n -> System.out.println(n));

Summary​

By defining and calling methods, you can build organized, reusable code. The enhancements in Java 8, such as default and static methods in interfaces and lambda expressions, empower developers to write more flexible, maintainable code. Understanding these elements helps in building complex, modular applications effectively.