← Blog

What is a subroutine in Java Programming? Java Tutorial 2024

By Umar Shakar · 7 Jul 2024 · Java

On this page

A subroutine is a named block of reusable code. In Java, the usual term is method: a method is declared inside a class and called when its task is needed.

Methods help you:

This guide covers method declarations, parameters, return values, and the difference between static and instance methods.

Syntax and declaration

A method declaration specifies a return type, a name, parameters in parentheses, and a body in braces. Modifiers such as public and static may come before the return type. Java does not use a method keyword.

public static int add(int first, int second) {
    return first + second;
}

A complete example

Save the following program as MethodExample.java. The main method calls add, stores the result and prints it.

public class MethodExample {
    public static int add(int first, int second) {
        return first + second;
    }

    public static void main(String[] args) {
        int total = add(4, 6);
        System.out.println(total);
    }
}

With a Java Development Kit installed, compile and run it:

javac MethodExample.java
java MethodExample

Expected output:

10

Parameters and arguments

Parameters are the named inputs in a declaration. Arguments are the actual values supplied in a call. In add(4, 6), the arguments are 4 and 6.

Java passes arguments by value. For an object, the value passed is a reference; a method may modify that object's state, but assigning a different reference to its parameter does not replace the caller's variable.

Methods that do not return a value

Use void when a method performs an action without returning a value. This complete program prints a greeting.

public class GreetingExample {
    public static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }

    public static void main(String[] args) {
        greet("Umar");
    }
}

Save as GreetingExample.java, then compile and run it:

javac GreetingExample.java
java GreetingExample

Expected output:

Hello, Umar!

Static and instance methods

A static method belongs to a class. An instance method is invoked on an object and can use that object's instance fields. Here, each Counter object keeps its own count.

public class Counter {
    private int count = 0;

    public void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }

    public static void main(String[] args) {
        Counter counter = new Counter();
        counter.increment();
        System.out.println(counter.getCount());
    }
}

Save as Counter.java, then run javac Counter.java and java Counter. Expected output: 1.

Method overloading

You can use the same method name for different parameter lists. Changing only the return type is not enough to overload a method.

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

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

    public static void main(String[] args) {
        System.out.println(add(2, 3));
        System.out.println(add(2, 3, 4));
    }
}

Save as OverloadExample.java, then compile and run it:

javac OverloadExample.java
java OverloadExample

Expected output:

5
9

Common mistakes

Keep each method focused on one task, use names that describe its behavior, and check it with representative inputs. Start with the complete examples above, then change the arguments to observe the results.

Need a website, SEO or a custom business app?
I'm Umar, a web developer and SEO specialist in Abu Dhabi. Get in touch.