/*In the while loop an object is created using the input.next with answers from the user and sending the info to the constructor of a class which creates an object. In the loop then I want to add the numb to the arraylist x each time. Would this be the code next in line to get the object into the arraylist or can I do that?
and would the object numb be added each position in the arraylist be ok or would the object need to be named something else since it is a reference and all the numbs in the arraylist would point to the same object? If this can't be done then how to I name the next object in line something else the way it is?
Originally posted by brian39:
and would the object numb be added each position in the arraylist be ok or would the object need to be named something else since it is a reference and all the numbs in the arraylist would point to the same object?*/
Your code is fine.
numb is a reference to an object. When you callyou add a copy of that reference to the ArrayList, so you now have two references to the same object (numb and the first element in the ArrayList). The next time round the loop, whenis executed, numb will now point to the new object and the first element of the ArrayList will still point to the first object you created. You then call again. You now have two references to the second object you created (numb and the second element of the ArrayList) and one reference to the first object you created (the first element of the ArrayList) and so on and so on.
Originally posted by Brian Lemieux:
I did not think there could be a reference variable with the same name. To me it means it would be pointing to the same object. In this case it appears from what I am understanding that the references even though having the same reference name will be pointing to a different object. Ok!
Okay, I see what you are saying - because you have the declaration of numb inside the loop a new local variable does get created each time, but the old ones are always discarded. You only ever have one numb variable. There isn't actually any need to declare numb each time - you could just declare it once before the loop