I've got a piece of code
list.add(new Person())
Since the Person object is missing it's naming part
Person p = new Person()
What kind of name does this person object get assigned to?
Is it important to know the name of the object? How would I go about accessing this object's methods if it is added to the ArrayList without a name?
edit: name == reference. I know objects don't have names
p
is not "the name of the object". p
is a variable that happens to hold a reference to the Person
object. Objects don't have names.
If you skip declaring the p
variable and add the Person
object directly to the ArrayList
, the ArrayList
still holds a reference to that object, so it doesn't matter whether you use list.add(new Person())
or list.add(p)
.
Either way you can retrieve the reference to the Person
with list.get(index)
, and then execute any methods you wish.