I'm trying to build an array of object references in a class called "PersonTest" that reference a class called "Person". Class "Person" has a variable "String name" and an array "String friends". Once I've created this array of object references, however, I can't seem to get the friends array of that respective object. It only returns the most recently created object's friends array. I use an object reference in class "PersonTest" to call the getFriends method contained in class "Person" to get a respective persons friends by using "people[x].getFriends()" but it only accesses the last object created regardless of the index "x". Please find code below. It includes extensive print statements that I hope illustrates what I'm trying to do. Thank you in advance for your help.
Everytime you created a new instance of Person you will give the same reference to array of friends into the Person object. So if this array of friends is changed it will change every Person's friends. If you want to make each Person have their own friends then you should instantiate the array of friends in your outer for loop instead of in the main() method. This will give you a new array of friends for each Person object.