Java error missing return statement

Ads
 

Java error missing return statement

Java error missing return statement are those error in Java that occurred when a programmer forget to write a return statement inside the conditional clauses. method with a non-void return provides you an object of the specific type.

Java error missing return statement are those error in Java that occurred when a programmer forget to write a return statement inside the conditional clauses. method with a non-void return provides you an object of the specific type.

Java error missing return statement

     

Java error missing return statement are those error in Java that occurred when a programmer forget to write a return statement inside the conditional clauses. method with a non-void return provides you an object of the specific type.

Understand with Example

In this Example we want to describe you a code that explain you  in understanding Java error missing return object. For this we have taken a non void method 'operation( )'that return you an int type and this method include a  for loop clause that check the condition If there is anything other than void method, then you must specify the return type in it. The given below code is missing a return statement in the non-void method so, whenever the compiler execute the code the code returns a missing return statement error.

There are two way to resolve this problem-

1)Change the non-void method to a void method.

2)Add or write a return expression to the non-void method.

Missingreturnstatement.java


public class Missingreturnstatement {

  public int operation(int num) {
  for (int i = 0; i <= num; i++) {
  System.out.println(i);
  }
  
  }

  public static void main(String[] args) {
  Missingreturnstatement m = new Missingreturnstatement();
  m.operation(7);
  }
}


Output

Compiling source file to /home/girish/NetBeansProjects/errors/build/classes
/home/girish/NetBeansProjects/errors/src/
Missingreturnstatement.java:
9: missing return statement

  }
error
BUILD FAILED (total time: seconds)


To resolve this error you have to give the return statement as return num;

Download code

Ads

Ads