Read Also: Fix illegal start of expression error in java
[Fixed] Missing return statement in java error
1. Method missing the return statement
public class HelloWorld
{
public static String printHelloWorld() {
System.out.println("HelloWorld from JavaHungry!");
}
public static void main(String args[]) {
printHelloWorld();
}
}
I am using the HelloWorld java program as an example. If you compile the above code using below command
javac HelloWorld.java
you will get the missing return statement error. There are two ways to fix it.
1.1 In printHelloWorld() method we have declared return type as String. But if you notice there is no return statement inside the printHelloWorld() method. Just add that as shown below.
public static String printHelloWorld() {
System.out.println("HelloWorld from JavaHungry!");
return "Alive is Awesome";
}
Now compile and run your program using commands below
javac HelloWorld.java
java HelloWorld
Yahoo!! your problem is resolved.
HelloWorld from JavaHungry!
1.2 The above situation can also be solved by changing the return type to void without providing any return statement.
public static void printHelloWorld() {
System.out.println("HelloWorld from JavaHungry!");
}
Now compile and run your program using below commands
javac HelloWorld.java
java HelloWorld
Yahoo!! your problem is resolved.
HelloWorld from JavaHungry!
2. Missing return statement error within if / while / for
If you declare return statement inside if / while / for but not at the end of the method containing them, then you will get missing return statement error as shown below./**
* Java program to demonstrate
* Missing return statement in java error
* inside if / while / for
* @author Subham Mittal
*/
public class HelloWorld
{
public static String printHelloWorld() {
int i = 0;
if (i == 0) {
return "inside if block";
}
for ( i=0; i < 9 ;i++) {
return "inside for loop";
}
while (i < 9) {
return "inside while loop";
}
//Missing return statement for method
}
public static void main(String args[]) {
printHelloWorld();
}
}
If you compile the above code then you will get missing return statement error.
Fix it by adding the return statement for the method similar to the one we did in case 1 above i.e adding line return "Alive is Awesome";
Note: Assuming the return type of method is not void. You must provide the return statement for the method, which should be the last statement in the method.
There is a special case, what happens when you provide return statements inside both if/else block, as shown below. What will be the output?
public class HelloWorld
{
public static String printHelloWorld() {
int i = 0;
if (i == 0) {
return "inside if block";
}
else {
return "inside else block";
}
}
public static void main(String args[]) {
printHelloWorld();
System.out.println("HelloWorld from JavaHungry!");
}
}
The above code will compile fine and you will get the following output:
HelloWorld from JavaHungry!
That's all for today. Please mention in comments in case you have any questions related to missing return statement in java error.
0 Comments