What is a subroutine in Java Programming? Java Tutorial 2024
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:
- Divide a program into smaller, understandable tasks.
- Reuse behavior instead of repeating the same statements.
- Test one piece of logic at a time.
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;
}
publiccontrols access to the method.staticassociates this method with its class rather than an instance.intis the return type.addis the method name;firstandsecondare its parameters.returnsupplies a value to the caller.
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
- Declaring a named method inside another method rather than inside the class.
- Omitting a return value from a method that requires one.
- Calling an instance method from a static context without an object.
- Passing arguments whose number or types do not match an available method.
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.
I'm Umar, a web developer and SEO specialist in Abu Dhabi. Get in touch.