Add to Google Reader or Homepage

cannot return a value from method whose result type is void


When a function is declared to be of void return type then the function would not return any value.If there is any return statement inside that function then this error would occur.

Program:


public class Er
{
public static void main(String args[])
{
add();
}
public static void add()
{
int a=10;
int b=5;
int c=a+b;
return c;
}
}

Error:

C:\blog\Er.java:12: cannot return a value from method whose result type is void
return c;
       ^
1 error

In the above code the function is of void return type. So having a return statement inside the function will throw an error.This can be resolved by either changing the return type of the function or by removing the return statement from the function block.
 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top