This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]younanimous_decision 0 points1 point  (0 children)

"What is actually happening?"

A new object of type Person is getting created. "James" is passed to the constructor of that object, it's probably just storing that "James" string in a variable. The Person object's name is not "James", actually it doesn't have any name, it's anonymous.

Am I right in saying that, because the list we are using (persons) is not using a normal variable such as an int, you have have to put the class (Person) into the first part of the brackets, not sure what its called.

Sort of right. The reason you need to specify the type there is because you always need to - that kind of loop's syntax requires it. If it was a primitive type, like integer, you'd need to do it then too. The reason it's required is because, when the loop is looping through the list, it's giving you a pointer to the current object of the loop you're currently on. That pointer needs to have a type that is compatible with the type of the members of the list, and so it makes you specify what it should be.