Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Hopefully my last question for today... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hopefully my last question for today..." Watch "Hopefully my last question for today..." New topic

Hopefully my last question for today...

Lisa Beglaw
Ranch Hand

Joined: Jul 16, 2005
Posts: 65

I wrote this incorrectly so that when someone invokes the rent out method, it will still add one to the times rented field even if the movie is rented out.

So I need to change it so that if currentlyRented already = true, it won't do anything. But I am not sure how I would make this happen.

I would like it to do nothing if it is already true, and if it is false, chget it to true and add one to times rented.

But how do I set something in a method to do nothing?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Note that "if (currentlyRented = true") sets it to true, while
"if (currentlyRented == true)" actually compares the two values.
"if (currentyRented)" does the same.

Now on to your question: There are two ways of doing nothing. Option 1 is to just add a comment. You could just have an empty if block, but that is confusing.

Option 2 is to rewrite the if statement to avoid the "do nothing part":

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
M Jay
Ranch Hand

Joined: Sep 21, 2004
Posts: 66
Hi Lisa,

To do this, you should replace:

???do nothing;

in your code with:


This way, if the movie is currently rented, then the method will just return without going any further.

Hope that helped. If anything above was unclear to you just give me a shout.


SCJP J2SE 1.4<br />SCBCD J2EE 1.3
Lisa Beglaw
Ranch Hand

Joined: Jul 16, 2005
Posts: 65
Jeanne - Again with the = vs ==. This is the second time today I have been caught on that one! Thank you!!!

As for the rest, thank both.

Jeanne - I really like your shortened version and will give that a try.

M Jay - if I have the word 'return' at the end...will it actually return something?

I am off to test both! Thank again!
M Jay
Ranch Hand

Joined: Sep 21, 2004
Posts: 66
Dearest Lisa,

Because your method has a return type of VOID, it cannot return anything. So return in such situation means "do nothing and finish executing this method".

Hope that was helpful.

Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Personally I would prefer Jeanne's version. In some circles using a return statement other than at the end of a method (which isn't even needed in a void method) is considered bad practise because it can make it confusing to understand what the method is doing.
I agree. Here's the link:
subject: Hopefully my last question for today...
jQuery in Action, 3rd edition