Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

methods that return void..

Thread view: 
Frances Del Rio - 03 Jun 2005 18:35 GMT
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 - 03 Jun 2005 18:48 GMT
> pls, what is syntax for methods that return void?
>
[quoted text clipped - 19 lines]
>
> 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
Frances Del Rio - 10 Jun 2005 15:00 GMT
>>pls, what is syntax for methods that return void?
>>
[quoted text clipped - 32 lines]
> 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
Bryce - 03 Jun 2005 20:35 GMT
>pls, what is syntax for methods that return void?
>
[quoted text clipped - 17 lines]
>
>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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.