It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question about return? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question about return?" Watch "question about return?" New topic

question about return?

Maxwell Wood
Ranch Hand

Joined: Feb 15, 2011
Posts: 49
when is it legal to use return;
other than in a catch block
Devaka Cooray
ExamLab Creator

Joined: Jul 29, 2008
Posts: 3785

Have you practically tried that?

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15043

You can use a return statement to return from a method. You can use it anywhere in a method - not just in a catch block (in fact, catch blocks don't have anything in particular to do with the return statement).

Ofcourse, if your method has a return type other than void, you must specify a value to return in the return statement.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ankit Garg

Joined: Aug 03, 2008
Posts: 9453

You can use a return statement in a constructor too...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
Ankit Garg wrote:You can use a return statement in a constructor too...

class ReturnInConstructor {

public ReturnInConstructor() {
return ;


I agree. Here's the link:
subject: question about return?
It's not a secret anymore!