Home > Archive > Java Help > June 2005 > methods that return void..
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
methods that return void..
|
|
| Frances Del Rio 2005-06-03, 4:00 pm |
|
pls, what is syntax for methods that return void?
for example cookie.setComment()...
I can do
cookie.setComment();
but how do you assign return to a var? and why does a method like this
return void (i.e., nothing, right?) when setComment it would seem to me
is a string (same for other methods to set values for cookies..)
I mean get an error here.. (since method doesn't return a string..)
string comment = cookie.setComment();
so how do you do this?
void comment = setComment()??
obviously got an error here too..... thank you...
Frances
| |
| Allan Bruce 2005-06-03, 4:00 pm |
|
"Frances Del Rio" <fdr58@yahoo.com> wrote in message
news:42a094e5$1_1@x-privat.org...
>
> pls, what is syntax for methods that return void?
>
> for example cookie.setComment()...
>
> I can do
>
> cookie.setComment();
>
> but how do you assign return to a var? and why does a method like this
> return void (i.e., nothing, right?) when setComment it would seem to me is
> a string (same for other methods to set values for cookies..)
>
> I mean get an error here.. (since method doesn't return a string..)
>
> string comment = cookie.setComment();
>
> so how do you do this?
>
> void comment = setComment()??
>
> obviously got an error here too..... thank you...
>
> Frances
If something returns void it means it doesnt actually return anything,
therefore it is a syntax error to try and assign nothing to a variable. If
you have a look at a method that returns void then you will probably see no
'return' in it, but if you do it will say 'return' and nothing else.
For your example, I would imagine setComment() takes a String as a
paramater, and the designer has decided to not return anything which makes
sense since all you are doing is passing a message in effect. Some
programmers never return void, instead they take the opportunity to at least
return a boolean for sucess/failure but this is a design choice.
Anyway, the short answer is you cannot store nothing in a variable,
therefore assigning the result of a method that returns void to a variable
doesnt work.
Allan
| |
|
| On Fri, 03 Jun 2005 13:35:14 -0400, Frances Del Rio <fdr58@yahoo.com>
wrote:
>
>pls, what is syntax for methods that return void?
>
>for example cookie.setComment()...
>
>I can do
>
> cookie.setComment();
>
>but how do you assign return to a var? and why does a method like this
>return void (i.e., nothing, right?) when setComment it would seem to me
>is a string (same for other methods to set values for cookies..)
>
>I mean get an error here.. (since method doesn't return a string..)
>
> string comment = cookie.setComment();
>
>so how do you do this?
>
>void comment = setComment()??
>
>obviously got an error here too..... thank you...
Not sure what you are asking, but void methods do not return anything,
so attempting to assign something a value from that kind of function
will not compile.
If you need to get the comment from the cookie, I'm willing to bet
there's a function getComment()..
--
now with more cowbell
| |
| Frances Del Rio 2005-06-10, 4:02 pm |
| Allan Bruce wrote:
> "Frances Del Rio" <fdr58@yahoo.com> wrote in message
> news:42a094e5$1_1@x-privat.org...
>
>
>
> If something returns void it means it doesnt actually return anything,
> therefore it is a syntax error to try and assign nothing to a variable. If
> you have a look at a method that returns void then you will probably see no
> 'return' in it, but if you do it will say 'return' and nothing else.
> For your example, I would imagine setComment() takes a String as a
> paramater, and the designer has decided to not return anything which makes
> sense since all you are doing is passing a message in effect. Some
> programmers never return void, instead they take the opportunity to at least
> return a boolean for sucess/failure but this is a design choice.
> Anyway, the short answer is you cannot store nothing in a variable,
> therefore assigning the result of a method that returns void to a variable
> doesnt work.
ok, many thanks for yr response.. I looked at other "set" methods in
Servlet API (specif. in HttpServletResponse & ServletResponse) and well,
they all return void.. I still think this is weird.. (but Bryce is
right, of course, specif. in my ex., there IS a getComment() method, and
probaby all the others have a "get" equiv. also..) again many thanks,
also to Bryce.. Frances
|
|
|
|
|