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 to make a method void or return questions 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 "to make a method void or return questions" Watch "to make a method void or return questions" New topic
Author

to make a method void or return questions

Chris Davies
Ranch Hand

Joined: Feb 26, 2003
Posts: 110
hi

can someone give me a short clear definition (and example) of the diffrence between a void method and a return type method ?

do all void methods never return a value ?

also why do you seomtimes preface a mthod name with its data type and other times you do not ?

ie

public int addFish (Fish f)

or

public void setCalorieContent(float f) (why not state the float type before the method declaration ?)

finally why can you declare some methods in a class using neither the reurn or void declaration ?

cheers

chris


One day all greenhorns will run free ...
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
When you see the text for a method it tells you first what classes can call this method (options: public, protected, private or 'blank'=> which means package).
Next it tells you what you get back when you call this method, or "void" to indicate that the method does not return anything.
Next you have the name of the method and in parentheses the type of 'thing' (if anything) the method requires to run, i.e. what you have to feed it to make it work.
-----------------------
So
public Toast toaster( BreadSlice oneSlice ) {
// methods not shown, but ends with the return of a
// Toast object
}
is a method named "toaster" that needs to be given a BreadSlice object when you call it and which returns a Toast object. the BreadSlice object is here referred to by the name "oneSlice", but it could just have well as been "bs" or "Fred", the name doesn't matter, the type does.


-----------------------
Below is a method named doubler that requires a int (referred to here by the name "doubleThis") primitive when called and retuns an int.
private int doubler( int doubleThis )
{
return ( 2 * doubleThis );
}
--------------------------
No return value example:

public void printName( String aName ) {
System.out.println( aName );
}
--------
No parameters, no return value:

public void printGreeting() {
System.out.println( "Howdy ya'll!!" );
}

--------------------

A special kind of method called a constructor is used to create an object. A constructor always has the same name as the class.

---------------

You asked why float can't be written before the method name in one method you listed; it can't because then it would be the kind of object returned by the method rather than the kind of object required by the method.
----------------
Your questions sound like you need to read the java basics in one or several books and take some time for the general concepts to settle in your mind. Take a look at the Javaranch Bunkhouse for excellent beginning Java titles.

[ August 16, 2004: Message edited by: Elouise Kivineva ]
[ August 16, 2004: Message edited by: Elouise Kivineva ]
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Well, methods are simple chunks of codes that can help you really simplify your codes and shorten them. (hey, after all this is the concept on which java is based).

Remember the good-old methods that we use in our daily coding life. eg. for strings we use subString, or nextToken for String Tokenizer (and its a massive list). Well, here subString(int,int) and nextToken() are functions. Now, imagine this, u've to code for a really sophesticated calculator (something to do with engineering). Here, u've got a complex equation that will be used several times during your entire process. Now, if you think that ur gonna type (or copy-paste) the entire equation again and again, as and when it is used, then u've gotta be crazy.

Instead think this.
You declare a method, which takes in all the variable that r required for the calculation. Inside the method u calculate the value using that "complex" equation. Finally, u return the resulting value.

So the method should look like

public float reallyComplexEquation(float variable1, float variable2, float variable3)
{
the complex equation goes here

return theResultingFloatValue;
}

now u keep calling this method wherever u need to calculate using that ugly equation.

This makes ur code look simpler and easier to understand, understood. Cool, huh.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
(Ignoring exceptions...)

can someone give me a short clear definition (and example) of the diffrence between a void method and a return type method ?

I'm not exactly sure what you mean - perhaps something along the following lines.

The declaration of methodA indicates that methodA does not return a value, indicated by the keyword void. So, any user of methodA cannot expect any value to come back after calling it.

The declaration of methodB indicates that methodB returns a value of type int. So, any user of methodB can expect that an int value will be returned after invoking methodB. The user of methodB could assign this return value to a variable, or place it anywhere an int value could be used.

do all void methods never return a value ?

If by "void method" you mean a method with a declaration similar to the example methodA above, then yes - they return nothing.

also why do you seomtimes preface a mthod name with its data type and other times you do not ?

public int addFish (Fish f)

or

public void setCalorieContent(float f) (why not state the float type before the method declaration ?)


Don't confuse the return type of a method with the arguments to the method.

In order to invoke the addFish method, the user must supply a value for the required argument, Fish f. When method addFish completes, it will return an int value to the user.The argument to the method is a value that the method being invoked will use. The return value of the method is a value that the user of the method is likely to use.

Can you figure out what the value of result is after running the following example?

finally why can you declare some methods in a class using neither the reurn or void declaration ?

This question isn't clear to me. I'm thinking that the answer might be, "You can't do that. A method declaration requires a return type."

Is anything becoming clearer, Chris?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Think of a non-void method as an automat, which returns a railroad-ticket, if you insert a coin.

Think of the void-function as a letter-box. You throw you letter inside and nothing is returned.

If you need a ticket, you will not throw your money into the letter-box.
If you want to send your letter, you will not insert it into the ticket-automat.
It will simply not fit the expected arguments, and throw an 'UnsupportedArgumentException'.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: to make a method void or return questions
 
Similar Threads
overriding
overloaded methods
Overloading
wrapper??
accessing static method