This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes use of return statement in method which return type is void Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "use of return statement in method which return type is void" Watch "use of return statement in method which return type is void" New topic
Author

use of return statement in method which return type is void

kandarp mehta
Greenhorn

Joined: May 04, 2010
Posts: 6
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
Consider Paul's rocket mass heater.
 
subject: use of return statement in method which return type is void