Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create Object in DoWhile Send to Arraylist

 
Brian Lemieux
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running a command line program. I am including just the things in the pgm that I think is needed to answer this question.



/*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?
*/
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"brian39", please check your private messages for an important administrative matter.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"brian39" please read the important administrative private message I just sent you.

CR
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
"brian39", please check your private messages for an important administrative matter.


Snap!
 
Brian Lemieux
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To all:

My publicly displayed name was not correct. I have since corrected it. They were right on it. Shame on me.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No prob. Thanks for the quick action.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Brian Lemieux
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Thanks!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Thanks!


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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic