This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Exit Sub code in Java 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 » Beginning Java
Bookmark "Exit Sub code in Java" Watch "Exit Sub code in Java" New topic
Author

Exit Sub code in Java

Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

Hi Friends..

iam an ex newbie vb6 programmer.. and i become a newbie Java Programmer today..

in vb6 i often use "Exit Sub" Method which would exit from a procedure..
how can i do like that in Java?..

i use that to avoid using Nested if..

Thanks in advance..

Sorry, perhaps my english language isn't too good.. Prepare for SCJP 6, Please God help me.. ☼
References : [Java.Boot] [JavaChamp] [JavaPrepare]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Just use "return;". Similarly, "return value;" is the equivalent of


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
Try not to do that. See our style guide particularly about avoiding return break and continue, for an explanation.

Try to organise your control statements so they are controlled by a boolean flag; when that flag becomes "false" the control statement will terminate and then the method will terminate. And, as Rob says, return any values required as the last line of the method.
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

i have created like this in my application :




i uses that to avoid nested if..
its could run well..

is there any a better way?, to make my code more readable.. sorry i confused..

Thanks in advance..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
Why are you always returning null? If you return nulls you are risking problems with NullPointerExceptions later on. If you always return null, why don't you give the method a void return type?
Don't use == true or == false ever.
Never
. . . if (b == true) . . .
Only write . . . if (b) . . .

Never
. . . if (b == false) . . .
Only write . . . if (!b) . . .

Never write an empty catch; that will hide any Exceptions, so you can conceal errors and not know they have happened.

You are calling a doSave method which calls doMyTransaction so you seem to have confusing method names.

What you actually want isIf you have a String.equals method, putting a literal first prevents possible null pointer problems.
If you are a beginner in Swing, don't get NetBeans to write your code; write it yourself by hand.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Here's another take:(Although the stack trace is probably more appropriately logged using a real logging mechanism.)
Leonardo Carreira
Ranch Hand

Joined: Apr 07, 2009
Posts: 489

@David Newton and @Campbell Ritchie

Thanks for your advice..
i just know that the void could has "return;"

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Campbell Ritchie wrote:/* Only one "if" because both previous "if"s were identical to each other. */


I know you're getting old Campbell, so pop your reading glasses on


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
There's only one thing worse than having Rob catch me making that sort of mistake . . .


And that's Joanne catching me out

Sorry.















Probably better to implement those "if" blocks with the || operator.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Campbell Ritchie wrote:Try not to do that. See our style guide particularly about avoiding return break and continue, for an explanation.

That's a whole discussion on its own, probably as old as the VI vs Emacs discussion*. I personally return in the middle of a method quite often. Agreed, refactoring is a bit harder, but most of my methods are so good they simply don't need refactoring
(In other words: I'll tackle that problem when it arises)


* VI of course!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
Rob Prime wrote: . . . most of my methods are so good they simply don't need refactoring
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
We'd better get this thread back on subject
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

In that style guide, one of the codes is this (its related to having braces in if blocks with only one statement)



Why is this style given as wrong and the following right



The only difference I can see is the position of the opening curly brace and I don't think the first style would be considered wrong. Maybe it is a mistake or I'm missing something...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
The JavaRanch style guide is very restrictive on some points. Whoever wrote it was quite definite that braces should match vertically, which I personally find much easier to read. I think (when I looked on Wikipedia) that is called BSD-Allman indentation but might be mistaken. The Sun style guide uses K&R indentation, so only one of those examples in correct in each style guide.

Anyway, we have had enough digressions on this thread already.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Campbell Ritchie wrote:Whoever wrote it was quite definite that braces should match vertically, which I personally find much easier to read.


Sorry for continuing on this topic but I couldn't stop myself. I also like the BSD-Allman indentation but I adapted myself to K&R indentation because the K&B book used that indentation so I thought that it was the standard. But now I feel relaxed that I'm not the only one who likes BSD-Allman indentation ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38068
    
  22
What a thread. Ankit changed the topic to {}, the one subject which causes more controversy than two returns in the same method (also included). Joanne caught me out over a silly mistake, and Rob said his methods are all wonderful.

The whole of JavaRanch in a single thread
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exit Sub code in Java
 
Similar Threads
Callout Handler - receiving delayed events on the same thread
Reading a file in current folder
JSR's Approved
java is dead
Need help with using arraylist