wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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: 14074
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use of return statement in method which return type is void
 
Similar Threads
String processing
missing return value error
return statement
Methods: parameters
Dead Code on return statement