it throws me error incompatible types: unexpected return value -- return 10; please correct it make note where i am doing wrong code?

//return rule break

class Fianalblck
{ public static void main(String... s) { //int show int x; { try { return 10; } catch(Exception e) { } finally { return 20; } } System.out.println(x); }
}
2

4 Answers

void means the method cannot return any value. if you wish to quit the application with exit code you can use System.exit().

 public static void main(String[] args) { System.exit(10);
}

refer Can a main method in Java return something?

Your main method is void meaning it can't return anything. If you wish to return a value, create another method that is a returnable type (boolean, int, double, String, etc.) then call that method from your main.

It looks like you need to start from the beginning, here is a link to a java basics tutorial.

Here is a tutorial on return.

I guess this is what you want to do:

class Fianalblck
{ private int func() { try { return 10; } catch(Exception e) { } finally { return 20; } } public static void main(String... s) { Fianalblck f = new Fianalblck(); int x; x = f.func(); System.out.println(x); }
}

You want to try whether 10 or 20 will be returned from a method.

In java, you have to declare a new method, and call that method with the instance of Fianalblck.

1

Main method cannot return a value directly.

 Error: Main method must return a value of type void in class first, please define the main method as: public static void main(String[] args)

But you can simply use return in a main method.

public static void main(String[] args){
...
...
...
return;
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like