DEV Community

Subash
Subash

Posted on Edited on

Methods in Java

What is a Method?

A method is a block of code that performs a specific task. We can create a method once and call it whenever required.

Methods help us to reuse code and avoid repetition.

How to Create a Method?

Syntax:

accessModifier returnType methodName(parameters) {
    // Method body
}
Enter fullscreen mode Exit fullscreen mode

Example:

public class Main {

    static void greet() {
        System.out.println("Hello World");
    }

    public static void main(String[] args) {
        greet();
    }
}
Enter fullscreen mode Exit fullscreen mode

Types of Methods

1) Predefined Methods

Methods that are already available in Java libraries.

String name = "java";

System.out.println(name.toUpperCase());
System.out.println(name.length());
Enter fullscreen mode Exit fullscreen mode

2) User-Defined Methods

Methods created by programmers to perform specific tasks.

static void display() {
    System.out.println("Learning Java");
}
Enter fullscreen mode Exit fullscreen mode

Parameters and Arguments

  • Parameters: Variables declared in the method definition.
  • Arguments: Actual values passed when calling the method.
static void add(int a, int b) {
    System.out.println(a + b);
}

add(10, 20);
Enter fullscreen mode Exit fullscreen mode

Here, a and b are parameters, whereas 10 and 20 are arguments.

Return Type

A return type specifies the type of value a method returns.

static int add(int a, int b) {
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Here, int is the return type, and the return keyword sends the result back to the calling method.

Void Method

When a method doesn't return any value, we use the void keyword.

static void display() {
    System.out.println("Hello Java");
}
Enter fullscreen mode Exit fullscreen mode

Static and Instance Methods

1) Static Method

A static method belongs to the class. We can call it without creating an object.

static void display() {
    System.out.println("Static Method");
}

Main.display();
Enter fullscreen mode Exit fullscreen mode

2) Instance Method

An instance method belongs to an object. We need to create an object to call it.

void display() {
    System.out.println("Instance Method");
}

Main obj = new Main();
obj.display();
Enter fullscreen mode Exit fullscreen mode

Method Overloading

Method overloading means creating multiple methods with the same name but different parameters.

static int add(int a, int b) {
    return a + b;
}

static int add(int a, int b, int c) {
    return a + b + c;
}
Enter fullscreen mode Exit fullscreen mode

We can overload methods by changing the number, type or order of parameters.

Note: Changing only the return type is not method overloading.

Method Signature

A method signature consists of the method name and parameter types.

Example:

static int add(int a, int b)
Enter fullscreen mode Exit fullscreen mode

Method signature: add(int, int)

Varargs

Varargs (Variable Arguments) allows a method to accept multiple arguments.

We use three dots (...) to declare it.

static void display(int... numbers) {

    for (int n : numbers) {
        System.out.println(n);
    }
}
Enter fullscreen mode Exit fullscreen mode

Recursion

Recursion is a process in which a method calls itself.

static int factorial(int n) {

    if (n == 1)
        return 1;

    return n * factorial(n - 1);
}
Enter fullscreen mode Exit fullscreen mode

Here, the method calls itself until the base condition is reached.

Pass by Value

Java always follows pass by value. It means a method receives a copy of the value passed to it.

static void change(int number) {
    number = 100;
}

public static void main(String[] args) {

    int value = 50;

    change(value);

    System.out.println(value);
}
Enter fullscreen mode Exit fullscreen mode

Output:

50
Enter fullscreen mode Exit fullscreen mode

Changing the copied value inside the method doesn't change the original variable.

Important Points

  • Methods help us reuse code.
  • void methods don't return values.
  • Static methods belong to classes.
  • Instance methods belong to objects.
  • Method overloading allows the same method name with different parameters.
  • Java always follows pass by value.
  • Recursion means a method calling itself.

Top comments (0)