• Post Reply Bookmark Topic Watch Topic
  • New Topic

use of return statement in method which return type is void  RSS feed

 
kandarp mehta
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method which return type is void. But if I put return statement in that method, java will accept it. I just want to know that what is the use of return statement in void method?



Please help me to know this java future.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16048
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, it's useless; the method returns automatically from the method anyway, so there's no need to add a return statement explicitly.

However, sometimes you want to return before the end of the method. To do that, you can add a return statement. The only difference between doing this between a void and non-void method is that in the void method, you don't add an expression after the return keyword.

Simple example:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!