File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Format for a Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Format for a Method" Watch "Format for a Method" New topic

Format for a Method

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
I had a question on the proper format for a method.
I have an "item" object in a supply order system. One of its methods accepts a float value named priceChange as an argument and then modifies the object float variable itemPrice. I also want the method to display the value of the new price. Would the method look like this?
public float changeItemPrice(float priceChange){
this.itemPrice = itemPrice + priceChange
return itemPrice
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Looks like a start. A few things to think about ...
Any code after return will not execute. It probably will not compile as the compiler will complain that the code is unreachable. So you need to reverse the last couple lines there to print, then return.
Printing in every method will wind up with lots of output! This might be just fine in an exercise but in real life you'll want a logging class that can turn messages on and off so you don't blow up the machine with log files. But let's agree this Systme.out is exactly what you want right now. I always put classname and method in debugging messages: "MyClass.changeItemPrice Delta=" + argument + " NewValue=" + newvalue. When you get lots of classes and methods printing it helps to tell them apart!
Finally, there were some other recent posts - don't know if you were involved - about using float for money. It will make you unhappy in the long run because floats don't round and keep precision they way you'd like for dollars & cents or whatever your currency might be. You might be happier using ints and dividing out the decimal place only when you have to display something. That is, $1 would be stored as 100 and formatted to 1.00 only for display.
Hope that helps!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
Thanks for the reply Stan. You made me aware of some issues I didn't think about.
So what exactly does the return statement in a method do?
Does it simply exit the method? If so why is the return statement necessary?
What I intended to do with the System.out.prntln statement was to display the new item price to the user. I haven't dealt much with Swing yet, but its possible I needed to something involving it instead. What is thepurpose of the System.out.println statement?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
The "return" statement causes an immediate exit from the current method, back to wherever the method was invoked.
If the method doesn't return a value then the signature will be something like:

You can have multiple "return" statements in the method, but there can be no executable code immediately following a "return;" since it would be unreachable. And every possible path through the code must end with a "return;" statement.
If your method returns a value then the signature will be something like:

As with the previous example, you may have multiple "return ..." statements, but every one must return an object of type "String".
As for your question about "System.out.println(...)", this method simply prints (displays) some text to an output device. By default "System.out" will be the console (DOS command line, Unix shell, etc.) but it can be re-directed to a disk file or some other PrintStream.
One of its main uses is to assist in debugging. You scatter them about the code so you can see what is happening during the execution. In the code you provided, you'd need to move the "System.out.println(...);" so it's before the "return itemPrice;", as Stan explained.
I agree. Here's the link:
subject: Format for a Method
It's not a secret anymore!