aspose file tools*
The moose likes Beginning Java and the fly likes return Boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return Boolean" Watch "return Boolean" New topic
Author

return Boolean

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
I have the following method in my Current class:



This method is getting the results I expect but I am a little confused with the Boolean return statement as it is not actually being used anywhere?

The third line from the bottom:
this.newBalance = newBalance - amount;

is returning what I require i.e. the newBalance which is going into the method:



What I don't therefore understand is why is the method withdrawal returning a Boolean for allowed?

Is it because I should NOT be putting; in the method withdrawal:
The third line from the bottom:
this.newBalance = newBalance - amount;

instead, should I be putting something in the setBalance method like:




When I tried this though, I didn't get the expected results. If fact the newBalance didn't take account of the withdrawal amount.???

Thanking you in advance!
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624


What I don't therefore understand is why is the method withdrawal returning a Boolean for allowed?

What would happen if I have a balance of $100, and try to withdraw $1,000? Does the withdraw happen? And Should it happen? Read through (i.e. trace through it) the first withdraw method you posted and see what happens. Does the withdraw occur? And what is returned as a boolean value?
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

By the way, be careful when referring to the boolean return. You stated in both your subject line and in your question, that I quoted above, the method is returning a Boolean (capital 'B'). It is however returning a boolean (lower case 'b'). A boolean is a primitive type, whereas a Boolean is an class; a wrapper class for the boolean primitive. All the primitive types in Java have such wrapper classes, and the wrapper class' name is usually the same as the primitive, except they start with a capital letter. The one exception is int has a wrapper class of Integer. If you have not learned about these wrapper classes, you will. In the meantime you can read about them in the api docs.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

It seems to me that the intended use would be something like this:




The "allowed" variable was not meant to be used inside the withdraw method; it was meant to indicate a status, i.e. whether the withdrawal was allowed or not.


Junilu - [How to Ask Questions] [How to Answer Questions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Maureen Charlton:

(snip)

What I don't therefore understand is why is the method withdrawal returning a Boolean for allowed?

Is it because I should NOT be putting; in the method withdrawal:
The third line from the bottom:
this.newBalance = newBalance - amount;



I think the confusion is with terminology. This line of code does NOT return anything. It sets a member variable named "newBalance" which can be later retrieved with the getBalance() method. To return a value, you HAVE TO use the "return" keyword. So look through the withdraw() method and try to find where you use the word "return". This will show you what the method is returning directly. It should also indicate why this methead returns boolean (note the lower case "b"), rather than double.

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return Boolean