*
The moose likes Java in General and the fly likes Can someone explain this to me (void methods)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Can someone explain this to me (void methods)?" Watch "Can someone explain this to me (void methods)?" New topic
Author

Can someone explain this to me (void methods)?

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Hi guys/gals,

can someone explain to me this little understanding problem I have with void methods. I wrote a test class below to help with my issue. How come when I pass an arraylist to the void 'addNumberToList' method, it adds the number 2 to the arraylist even though its not returned?

I used two other variables, an int and Integer (to see if the wrapper class made a difference) and tried to change their value by doubling them into the void 'doubleInt' method. It doesn't work.



I ask because I am writing some card games at the moment and I want to use a 'GameManager' class that will shuffle a 'Deck' filled with 52 'Cards' and deal them cards to a list of 'Players'. Should the shuffle and deal methods be void or not? I am trying to write the whole program (as much as i can) on paper first before I start hacking away! I'm just having trouble deciding if methods should be void or return (which sounds stupid!). Going by the example above I should be able to just use void methods like so (this is just an example):



Thanks; this forum has always been helpful to a struggler!
Alan
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

When you call addNumberToList(arrayList), you're passing a (copy of a) reference to the ArrayList. Which means that any modifications you make to the list within the method are being made to the same list that's referenced in the main method.

When you try it with an int, it's still passing a copy, but now it's passing a copy of the actual value. There is no way to affect the original value held in the main method.

The void just means it doesn't return a value, but that doesn't mean it can't have side-effects. In fact, a void method that doesn't have side effects is pretty pointless.

For your card game, you're right, those methods don't need to return anything. In fact, check out the signature of the Collections.shuffle method, which can randomise the entries in a List:
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Matthew Brown wrote:
When you try it with an int, it's still passing a copy, but now it's passing a copy of the actual value. There is no way to affect the original value held in the main method.


So your saying that the value of x gets passed, not x itself. And the ArrayList itself (or a reference to the actual array list outside the method) gets passed in the other method?
Besides I don't think I could use a return for this method anyway:


I wouldn't be able to assign anything using a return value because the player gets dealt cards and the deck loses cards, changing both objects list of 'cards' attributes.

Thanks a bunch!
 
GeeCON Prague 2014
 
subject: Can someone explain this to me (void methods)?